如何将属性从SVG复制到另一个?

时间:2018-07-25 14:29:26

标签: python xml svg

我有两个SVG,它们的位置相同,但是我在其中更改了一些值,例如:

SVG1

<desc
   id="desc20622">[Visualization]
       name=H131B1;
</desc>

另一个是:

SVG2

<desc
   id="desc20622">[Visualization]
       name=R131C2;
</desc>

现在,我已经在一个SVG中重新分配了许多元素,我想将此更改复制到另一个SVG中。使用这些SVG,比较ID,将值从SVG2复制到SVG1并保存新的SVG文件的最简单方法是什么? 我对许多编程语言都很熟悉,但是我正在研究Python使用minidomxml.etree.ElementTree来完成这项工作。

有人可以帮我吗?预先感谢。

1 个答案:

答案 0 :(得分:0)

我想出了如何用Python自己做到这一点。

import xml.etree.ElementTree as ET

tree1 = ET.parse('SVG1.svg')
root1 = tree1.getroot()

tree2 = ET.parse('SVG2.svg')
root2 = tree2.getroot()

for child1 in root1.iter('desc'):
    for child2 in root2.iter('desc'):
        if child1.attrib == child2.attrib:
            child1.text = child2.text
            break

tree1.write('output.svg')

只需解析两个SVG,在每个desc上进行迭代,比较ID并复制文本!