我正在使用ElementTree修改microsoft Visio文件。我解析该文件,搜索“文本”并对其进行了修改。但是,将所有内容打包回MS Visio,文件已损坏。 ElementTree似乎重写了树中所有我没有要求的东西。
代码基本上是:
tree = ET.parse(filename)
root = tree.getroot()
for content in root.iter('{http://schemas.microsoft.com/office/visio/2012/main}Text'):
for child in content:
newText = modified_child.tail
child.tail = newText
tree.write('newfileName.xml',encoding="UTF-8",xml_declaration=True)
最后一行包含选项,这是因为ET如果没有选项,则将重音符号插入为Unicode。
结果是一个文件,其中树的许多条目都不相同。 例如
<Cell N='ShapeShdwOffsetY' V='-0.0625' U='IN' F='THEME("ShadowYOffset")'/>
成为
<Cell F="THEME("ShadowYOffset")" N="ShapeShdwOffsetY" U="IN" V="-0.0625" />
,因此引号变为"
,并且条目取反。如何告诉ElementTree保留除修改后的文本以外的所有内容?