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.