Reputation: 2336
Can I edit the Header & Footer of an existing Presentation using python-pptx? The values I want to set are as shown in the attached image. Thanks.
Upvotes: 7
Views: 3120
Reputation: 105
I asked this a long time ago, but I can't remember where and couldn't find it on SO. Scanny answered the question, so I'm relaying his answer here (probably poorly).
By default, Python-pptx doesn't include footers or page number placeholders when listing slide placeholders. It's common practice to recommend inserting text boxes instead when these are needed, but that's not useful when dealing with multiple templates or layouts.
The first thing you'll need to add somewhere is a patch so that the placeholders are included:
def footer_patch(self):
for ph in self.placeholders:
yield ph
SlideLayout.iter_cloneable_placeholders = footer_patch
You should then be able to grab the footer from the placeholders with simple means:
footer_copy = "Hi, it's me, the footer"
elif "FOOTER" in str(shape.placeholder_format.type):
footer = slide.placeholders[shape.placeholder_format.idx]
footer_text_frame = footer.text_frame
insert_text(footer_copy, footer_text_frame)
The above is old code, and probably a poor example of how to do this, but I hope it gives a starting point. A similar approach should work for the other values you listed there. Some values, like the page number, may require additional XML editing, which you can read about in another post where Scanny was my savior.
Please note, if you're using placeholders for other tasks, adding the Footer placeholder to the list of placeholders may have unforeseen consequences.
Upvotes: 2