7

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.

Powerpoint Header and Footer

Vishal
  • 2,097
  • 6
  • 27
  • 45
  • I tried to add a footer in the template ppt but it will not show in the new ppt I create with `python-pptx`. – Peter Chen Jun 06 '19 at 14:30
  • These may be relevant: https://github.com/scanny/python-pptx/issues/64 and https://python-pptx.readthedocs.io/en/latest/dev/analysis/placeholders/master-placeholders.html – FZS Jul 13 '23 at 09:01

1 Answers1

2

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.

emmartin
  • 105
  • 12