I'm currently working on a PowerPoint template with python-pptx. I have created a template on MS Office and would like to dynamically populate the template with both text and images. Therefore, I have created layouts and slides containing placeholders.
I have no problems accessing and populating the text placeholders, however, I'm struggling with the picture placeholders.
I would like to use the .insert_picture()
method of the PicturePlaceholder object. You can also find an example of this method in the documentation here.
To do so I am accessing my placeholders as follows:
prs = Presentation(pathToPptxTemplate)
slide = prs.slides[indexOfMySlide]
# There are multiple image placeholders on my slide
image_placeholders = [
i for i in slide.placeholders if
i.placeholder_format.type == 18
]
for index, shape in enumerate(image_placeholders):
shape.insert_picture(pathToMyImage)
This results in the following error message:
AttributeError: 'PlaceholderPicture' object has no attribute 'insert_picture'
After scratching my head for a few minutes, I noticed that I was looking at the documentation for PicturePlaceholder
but accessing a PlaceholderPicture
object. The difference I read in the documentation is that:
whereas
Now I have not populated the Placeholder with any picture yet. I have just created an empty placeholder in PowerPoint.
So here is my question:
How can I create a PicturePlacholder in PowerPoint and how can I access it with Python-pptx ?
If this is not possible, do you have an alternative solution ?
Thanks in advance
Try deleting the offending "placeholder" shape and inserting a new, untouched placeholder using the PowerPoint UI. On my Mac with PowerPoint 2016 this is:
You can adjust the size and position if you want, but otherwise leave it untouched and try again. Probably best to do all this with a new presentation and delete all but one of the slide layouts and all the slides, just to keep things simple.
Then use python-pptx
to create a slide from that layout:
slide = prs.slides.add_slide(prs.slide_layouts[0])
I think you're going to find that works and there is some way that your template placeholder has had an image inserted into it, perhaps to act as an example or something.
A couple possible areas of confusion:
A user does not create a placeholder shape on a slide. A slide inherits the placeholders of its slide layout when the slide is created.
A PicturePlaceholder
shape has the element <p:sp>
, as do all placeholders. When a picture is "inserted into the placeholder" the p:sp
element is replaced with a <p:pic>
element; the placeholder shape is replaced with a picture shape.
I think what you'll find is that the picture placeholder on your template slide has already been "used" and is now a picture. You can confirm this by inspecting the xml with print(shape.xml)
.
The solution would be to fix that slide by recreating it, or it might work to open it in PowerPoint and delete its contents (not the whole shape) which should return it to its original picture placeholder state. It might also work to delete the offending shape and then "re-apply Layout" (possibly "Reset" in the slide layout area of the Home toolbar).