Reputation: 763
My md2pptx code creates slides using python-pptx. It sometimes ignores shapes on a page it doesn't need.
In Powerpoint Slide Show these empty shapes don't appear. In LibreOffice they seem to.
I'm pretty adept at manipulating the underlying XML for a slide.
Is it feasible to remove empty shapes - perhaps by deleting their XML elements? Or does python-pptx itself offer the capability to delete a shape? (I think not.)
Assume I can navigate to the shapes and figure out which ones are empty.
Note: I'm not aiming to delete whole slides, just empty shapes.
Upvotes: 0
Views: 1111
Reputation: 28873
Deleting a "stand-alone" shape is reliable and pretty easy, something like:
sp = shape._element
sp.getparent().remove(sp)
The problem comes in where the shape has a relationship to some other "package part". For example, a Picture
shape has a relationship (identified with an rId
) to an image part (file) in the package (.pptx zip archive). In those cases, if you don't also properly deal with the relationship, you may get a "repair error" when you try to open the resulting file in PowerPoint.
A "regular" shape (so-called "auto-shape") such as a rectangle, text-box, line, or other geometric shape has no relationships and can be reliably deleted with this method. A table is probably safe too, but not a chart. A group shape is probably okay too, but only if it does not contain a picture or a chart. Both a picture and a chart may be a problem if you don't also remove their relationship.
Whether or not a repair error is triggered is a behavior that may differ between PowerPoint and LibreOffice (or other PPTX client). You can try just deleting a picture or chart shape without dealing with the relationship and see what happens, but to be reliable you'd need to test it with all the possible clients.
Removing a relationship is a little more involved and is either covered in another python-pptx
question here on SO or would make a good new question.
Upvotes: 3