Playing around with Python PPTX and it seems that it is not reading in the slide master name properly.
You can see here I rename the slide master
and it even shows up when trying to add a slide
However when I load the presentation through pptx
the name is ''
.
In [14]: import pptx
In [15]: pres = pptx.Presentation("adsf.pptx")
In [16]: pres.slide_master.name
Out[17]: ''
Maybe I am doing something wrong from the powerpoint side. I'd love to know. I am using Office 2016. As a side note, I was digging around the xml and it appears the slidemaster xml isn't holding any attributes related to the name attributes in the ooxml
. However the theme xml does. Color me confused.
Thanks for your time and efforts
EDIT:
After all this I have found my solution and would like to thank @Scanny
import pptx
import re
from lxml import etree
# This causes pres.slide_master.layout.placeholder.name to be passed to pres.slide.placeholder
@property
def placeholder_name(self):
"""Name of the placeholder inherited from slide master"""
return self._inherited_value("name")
def Presentation(powerpoint=None):
"""
Return a |Presentation| object loaded from *pptx*, where *pptx* can be
either a path to a ``.pptx`` file (a string) or a file-like object. If
*pptx* is missing or ``None``, the built-in default presentation
"template" is loaded.
"""
if powerpoint is None:
powerpoint = pptx.api._default_pptx_path()
# get the package and not just the presentation package
package = pptx.package.Package.open(powerpoint)
# now extract the document
presentation_part = package.main_document_part
if not pptx.api._is_pptx_package(presentation_part):
tmpl = "file '%s' is not a PowerPoint file, content type is '%s'"
raise ValueError(tmpl % (powerpoint, presentation_part.content_type))
# the theme names are the slide master names
themes = (part for part in package.parts if re.search("^/ppt/theme/theme\d+\.xml$",part.partname))
theme_names = [etree.fromstring(theme.blob).get("name") for theme in themes]
# now get the presentation
presentation = presentation_part.presentation
# change the slide master names
for idx,sld_mstr in enumerate(presentation.slide_masters):
sld_mstr.name = theme_names[idx]
return presentation
pptx.Presentation = Presentation
pptx.shapes.placeholder._InheritsDimensions.placeholder_name = placeholder_name
In [2]: pres = pptx.Presentation("adsf.pptx")
In [3]: for sm in pres.slide_masters: print(sm.name)
my master
number 3
my second
In [4]: layout = pres.slide_masters[0].slide_layouts[0]
In [5]: layout.name
Out[5]: 'my master title slide layout'
In [6]: new_slide = pres.slides.add_slide(layout)
In [7]: new_slide.placeholders[0].name
Out[7]: 'Title 1'
In [8]: new_slide.placeholders[0].placeholder_name
Out[8]: 'Main Title'
In [9]: pres.save("test.pptx")