0

I want to read all the footers from a ppt using python (python-pptx) and store it in different Strings. The documentation is not of much help. Can anyone help me with this?

    >> slide_placeholders = slide.placeholders
    >> len(slide.placeholders)
       4

    >> slide.placeholders[4]
       KeyError...

I was expecting to get the footer object and somehow read it.

1 Answers1

0

Slide.placeholders returns a _SlidePlaceholders object, which has unusual semantics because identifying placeholders can be tricky. Used as you have above, it has dict semantics with the key being the value of the idx attribute of the placeholder. This idx attribute is determined by the slide-layout the slide was created from and which was thereby the source of its placeholder shapes. These idx values are essentially arbitrary and are not guaranteed to be contiguous.

You can discover the idx values like this:

for placeholder in slide.placeholders:
    print(placeholder.placeholder_format.idx)

Now the question is: "Which of these placeholders is the one I want?"

There are a few strategies:

  1. Identify by idx value: A particular placeholder on two different slides formed from the same slide-layout will have the same idx value. If you can discover that idx value, using slide.placeholders[idx] is a good approach. Note that this is dict (key) lookup, not list (offset) lookup.

  2. Identify by placeholder type: Placeholders come in different types. https://python-pptx.readthedocs.io/en/latest/api/enum/PpPlaceholderType.html#ppplaceholdertype. If the one you want is the only one of that type on the slide, this can work

 from pptx.enum.shapes import PP_PLACEHOLDER

 def find_placeholder():
     for placeholder in slide.placeholders:
         if p.placeholder_format.type == PP_PLACEHOLDER.FOOTER:
             return placeholder
     print("not found")
  1. Find by z-order: This is generally not terrifically reliable because shapes can be moved forward and backward in z-order easily, but for completeness sake, say you figured yours was the last (topmost) placeholder:
# ---note: this is list-semantics item access, not dict---
placeholder = list(slide.placeholders)[-1]
scanny
  • 26,423
  • 5
  • 54
  • 80