python pptx is not capturing slide master names properly

Yeah, that’s how I would proceed, look for the string “my master” in the XML and see where it shows up.

A PowerPoint theme includes a master and layouts, so the UI could figure this rename operation for a rename of the theme, which could make sense from a UI perspective, like you want to save this theme and then use it later by picking this name from a list.

The reverse operation might make sense, meaning use python-pptx to set the master name, read it back out to confirm, and then see where that shows up in the UI afterward, if anywhere.


You can get to the presentation-part directly from the presentation:

prs = Presentation(...)
presentation_part = prs._part

All parts have a reference to the package, you don’t have to load it yourself:

package = presentation_part._package

But you probably don’t need that because you can get to the presentation theme(s) (as opposed to Notes-pages themes etc.) directly from the presentation part:

from pptx.opc.constants import RELATIONSHIP_TYPE as RT

theme_rels = [rel for rel in presentation_part.rels if rel.reltype == RT.THEME]
theme_parts = [presentation_part.related_parts[rel.rId] for rel in theme_rels]

Then just load each theme in as an XmlPart with something like:

theme_parts = [
    XmlPart.load(
        part._partname,
        part._content_type,
        part._blob,
        part._package,
    )
    for part in theme_parts
]

And then you can get the root of the theme XML document on theme_part._element and you can use lxml.etree._Element methods on that to traverse that tree, or just get the root element name with theme_part._element.attribs["name"].

This is all just aircode from memory, but hopefully gives you enough to go on and you can post working code once you get there.

If what you have does the job then by all means use it, but this is perhaps more direct and uses the implementations already there so you may have less to worry about things going unexpectedly wrong 🙂

Pretty much all the code this is exercising is in pptx/opc/package.py.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top