asdfkjasdfjk
asdfkjasdfjk

Reputation: 3894

Python pptx: how to find and delete the empty or unpopulated placeholders

I have created a presentation using python-pptx. However, some slides have the empty placeholders with default text click to add title/text that looks like the one below. enter image description here

So basically, there are non-empty placeholder with text Some texts here and other two are empty placeholders. How can I find and delete them? Also, there are same empty placeholders for images. How can I find them as well. Thanks

Upvotes: 4

Views: 3850

Answers (2)

Luv
Luv

Reputation: 1

For blank slide, adjust the layout.

prs.slide_layouts[6]

Refer to the documentation (https://python-pptx.readthedocs.io/en/latest/user/slides.html), to see the different slide layouts.

Upvotes: 0

scanny
scanny

Reputation: 28903

Finding them is fairly easy; something roughly like this should do the trick:

for placeholder in slide.shapes.placeholders:
    if placeholder.has_text_frame and placeholder.text_frame.text == "":
        print("found one %s" % placeholder)

Deleting it is harder, because there is no direct API support for deleting shapes. However, in this simple case of text placeholders, this should work:

sp = placeholder._sp
sp.getparent().remove(sp)

The ._sp attribute is the lxml etree._Element object representing the actual shape XML element (a placeholder <p:sp> element in this case). .getparent() and .remove() are methods on etree._Element and calling these manipulates the underlying XML directly. This can be dangerous, like if you tried this with a chart shape you'd probably get a repair error when you tried to load the presentation, but in this case it's safe enough.

Upvotes: 4

Related Questions