Python PPTX 模块 - 设置演示文稿循环直至退出

问题描述 投票:0回答:1

我正在尝试将 .pptx 演示文稿设置为循环播放,直到按下 Escape 键为止。通常这是在 Powerpoint 中的“设置幻灯片放映”对话框中完成的,但我是使用 python pptx 模块来完成的。我已经使用 opc-diag 进行了测试,并确定选择该选项时的唯一区别是 powerpoint 文件的 /ppt/presProps.xml 部分中有一个类似这样的标签:

<p:showPr loop="1" showNarration="1">

现在,在我的Python代码中,我已经尝试了一些东西但没有成功:

def xpath(el, query):
    nsmap = {'p': 'http://schemas.openxmlformats.org/presentationml/2006/main'}
    return etree.ElementBase.xpath(el, query, namespaces=nsmap)
    
presentation = Presentation()
p = "http://schemas.openxmlformats.org/presentationml/2006/main"
ele = etree.Element(etree.QName(p, "showPr"), attrib={'loop': '1'})

# this does not work... Expecting element, getting presentation. 
xpath(presentation, './/p:presentationPr')[0].getparent().insert(-1, ele)

我需要将 showPr 的元素附加到演示文稿的根元素,但 xpath 函数需要一个元素,其中我刚刚传递了演示文稿。我不知道如何访问根节点来附加 showPr 循环位。

如有任何帮助,我们将不胜感激。

我已经尝试了上面的代码,我希望它能在 /ppt/presProps.xml 文件中产生我想要的设置

<p:showPr loop="1" showNarration="1">
的结果,但它需要一个元素,并且我正在传递演示对象。我不知道如何继续获取

python powerpoint python-pptx
1个回答
0
投票

您的方向是正确的,但还有几个步骤:

  1. 您需要获取
    PresentationProperties
    “部分”,它是一个与
    Presentation
    部分不同的 XML 文档。 (.pptx zip 存档中的文件在 OPC 行话中称为 parts。)
from pptx.opc.constants import RELATIONSHIP_TYPE as RT

prs_part = presentation.part
prs_props_part = prs_part.part_related_by(RT.PML_PRES_PROPS)
  1. 您需要获取prs_props部分的根元素:
from pptx.oxml import parse_xml

presentationPr = parse_xml(prs_props_part.blob)

然后您应该能够针对该

presentationPr
元素运行 XPath 表达式。

这应该可以打印 XML 来查看它,这将确认到目前为止的步骤是否有效:

from pptx.oxml.xmlchemy import serialize_for_reading

print(serialize_for_reading(presentationPr))
© www.soinside.com 2019 - 2024. All rights reserved.