【问题标题】:Modifie the xml data in python在python中修改xml数据
【发布时间】:2014-05-18 08:41:59
【问题描述】:

我正在尝试编辑 pptx xml 文件的属性以在幻灯片之间添加幻灯片,我知道有一个名为 python-pptx 的模块,但它无法做到这一点。

所以一切都从文件“[Content_Types].xml”开始,我要做的第一件事是在/ppt/slides/slide3.xml 之后添加一个字符串<Override ContentType="application/vnd.openxmlformats-officedocument.presentationml.slide+xml" PartName="/ppt/slides/slide4.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>

关于如何做到这一点的任何建议?

【问题讨论】:

    标签: python xml powerpoint


    【解决方案1】:

    您不需要为此使用xml解析器,只需从文件中读出数据并insert您的行在您想要的位置,对于您的情况,可以在末尾添加数据文件没有在中间插入并移位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)
    

    【讨论】:

      猜你喜欢
      • 2022-06-23
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      相关资源
      最近更新 更多