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:
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.
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")
- 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]