How to Delete Unpopulated Placeholder Items Using Python-Pptx

This is very simple, but I cannot find the actual method anywhere in the documentation or otherwise.

I am using the python-pptx module and all I need to do is delete a single placeholder item, an empty text box, on some slides (without having to create a completely new layout just for these slides) - the closest thing to an answer is here: under Unpopulated vs. populated but it still does not say how to actually delete/remove the placeholder.

I've tried all the obvious methods .delete(), .remove(), etc.

2 Answers

There is no API support for this, but if you delete the text box shape element, that should do the trick. It would be something like this:

textbox = shapes[textbox_idx]
sp = textbox.element
sp.getparent().remove(sp)

Using the textbox variable/reference after this operation is not likely to go well. But otherwise, I expect this will do the trick.

3

Here is a follow-up to scanny's answer based on the helloworld example in the docs. The code deletes the subtitle placeholder, which is empty in this case.

from pptx import Presentation

prs = Presentation()

title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)

title = slide.shapes.title
title.text = "Hello, World!"

subtitle = slide.placeholders[1]
sp = subtitle.element
sp.getparent().remove(sp)

prs.save('test.pptx')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest