Editing underlying PowerPoint XML with Python (python-pptx)

荒凉一梦 提交于 2019-12-14 01:16:07

问题


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.

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.

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)

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

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

E.g.:

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

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

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

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


回答1:


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.



来源:https://stackoverflow.com/questions/49560567/editing-underlying-powerpoint-xml-with-python-python-pptx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!