使用 Python (python-pptx) 编辑底层 PowerPoint XML [英] Editing underlying PowerPoint XML with Python (python-pptx)

查看:64
本文介绍了使用 Python (python-pptx) 编辑底层 PowerPoint XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 PowerPoint 文件,其中包含许多指向 Excel 文档中不同工作表的链接.我需要以编程方式更改这些链接指向的 Excel 文档.

I have PowerPoint files with many dozens of links to different sheets in an Excel document. I need to change the Excel documents to which these links point in a programmatic way.

我很确定我可以用 VBA 做到这一点,但由于我无论如何都在 python 中生成 Excel 文档,我更喜欢更新那里的链接.

I'm pretty sure I could do this with VBA, but since I'm generating the Excel documents in python anyway I'd prefer to update the links there as well.

我为测试 .pptx 文件挖掘了底层 XML 文件,发现链接引用位于 ppt/slides/_rels/ 文件夹中(解压后.pptx 文件)

I dug in to the underlying XML files for a test .pptx file and found that the link references live in the ppt/slides/_rels/ folder (after unzipping the .pptx file)

例如,slide1.xml.rels 包含多个关系,一个具有 TargetMode="External"Target="FULL_PATH_OMITTED\test.xlsx!Sheet1!R3C5:R20C14"

For example, slide1.xml.rels contains several relationships, one having TargetMode="External" and Target="FULL_PATH_OMITTED\test.xlsx!Sheet1!R3C5:R20C14"

使用 python-ppt 包我发现这个相同的引用存在于 slide.part.rels

Using the python-ppt package I found that this same reference lives under slide.part.rels

例如:

for rel in slides[0].part.rels.values():
    if rel.is_external:
        print(rel.target_ref)

为链接找到相同的路径(即"FULL_PATH_OMITTED\test.xlsx!Sheet1!R3C5:R20C14")

Finds the same path for the link (i.e. "FULL_PATH_OMITTED\test.xlsx!Sheet1!R3C5:R20C14")

如果可以更改,我不知道如何更改此值.只是尝试使用 python-pptx 设置它会产生 AttributeError

What I don't know how to do is change this value, if it can be changed. Just trying to set it using python-pptx produces an AttributeError

有没有办法使用 python-pptx 修改 PowerPoint 文件的底层 XML?或者一些替代策略会很好.

Is there a way to modify the underlying XML for a PowerPoint file using python-pptx? Or some alternative strategy would be fine.

推荐答案

尝试设置 rel (Relationship) 对象的 ._target 属性
https://github.com/scanny/python-pptx/blob/master/pptx/opc/package.py#L555

Try setting the ._target attribute of the rel (Relationship) object
https://github.com/scanny/python-pptx/blob/master/pptx/opc/package.py#L555

rel._target = 'FULL_PATH_OMITTED\test.xlsx!Sheet1!R3C5:R20C14'

这仅在关系类型为外部(而不是与同一包中其他部分的关系)时有效.

This will only work when the relationship type is External (as opposed to a relationship to another part in the same package).

当然,这是黑客内部结构,因此使用风险自负.也就是说,这部分代码库在很长一段时间内都非常稳定.

This is hacking internals, of course, so use at your own risk. That said, this part of the code base has been very stable for a long time.

这篇关于使用 Python (python-pptx) 编辑底层 PowerPoint XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆