15

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: http://python-pptx.readthedocs.io/en/latest/user/placeholders-understanding.html 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.

Jacristi
  • 153
  • 1
  • 5

2 Answers2

17

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.

scanny
  • 26,423
  • 5
  • 54
  • 80
  • 1
    Excellent - I only had to change what you gave slightly: textbox = **slide**.shapes[textbox_idx] with **slide** changing depending on the slide variable name. Thank you! – Jacristi Sep 21 '16 at 14:24
  • Hey, I know this is old but I'm curious if there is working functionality to turn All Caps on or off for text; I'm able to do everything else, such as bold, italcis, underline, etc. as per http://python-pptx.readthedocs.io/en/latest/user/text.html. Also mentioned on this same page is the ability to do this, but I cannot find it anywhere – Jacristi Oct 13 '16 at 18:15
  • @Jacristi: you should ask this as a separate, new question. It's not related to this topic. If you tag it with 'python-pptx' I'll see it and will be happy to respond. – scanny Oct 14 '16 at 05:48
1

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')
jrinker
  • 2,010
  • 2
  • 14
  • 17