我正在尝试编辑 pptx xml 文件的属性以在幻灯片之间添加幻灯片,我知道有一个名为 python-pptx 的模块,但它无法执行此操作。
所以一切都从一个文件“[Content_Types].xml”开始,我要做的第一件事就是把一个字符串 <Override ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml" PartName="/ppt/slides/slide4.xml"/>
之后/ppt/slides/slide3.xml
之间然后将其他幻灯片的数量增加 1
完整的 xml 看起来像这样
<?xml version="1.0" encoding="UTF-8" standalone="true"?>
-<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
<Override ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml" PartName="/ppt/slides/slide1.xml"/>
<Override ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml" PartName="/ppt/slides/slide2.xml"/>
<Override ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml" PartName="/ppt/slides/slide3.xml"/>
<Override ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml" PartName="/ppt/slides/slide4.xml"/>
<Override ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml" PartName="/ppt/slides/slide5.xml"/>
<Override ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml" PartName="/ppt/slides/slide6.xml"/>
</Types>
关于如何做到这一点有什么建议吗?
最佳答案
你不需要为此使用 xml 解析器,只需从文件中读取数据并插入
你的行到你想要的位置,对于你的情况,添加数据就可以了在文件的末尾没有插入它在中间移动1,因为你的xml没有子树,反正结果是一样的
with open('[Content_Type].xml', 'r') as fl:
readout = fl.read()
spl = readout.split('><')
spl.insert(len(spl)-1, """Override ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml" PartName="/ppt/slides/slide%d.xml"/""" %(slides+1))
final = '><'.join(spl)
with open('[Content_Type].xml', 'w') as fl:
fl.write(final)
关于python - 修改python中的xml数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23720095/