I would like to distribute a pptx file with a Python script that adds customized graphs using python-pptx. To customize the graphs, I would like to have a slide that acts as a form to allow users to set some values. I can add combobox's and listbox's to a slide using the controls shown on the Developer tab but I'm not sure if python-pptx can access the values set by those controls. I would rather not use VBA macros because then the presentation would need to be a pptm file rather than a pptx file and many users have organizations that don't allow macro files from Microsoft Office. Is it possible for python-pptx to access the values set by the user when combobox and listbox controls are on a slide and is it possible to set the list of options shown in those controls?
1 Answers
There is no API support for that in python-pptx
, not yet anyway.
However, if an item is stored in the .pptx
file, it can be accessed in Python if you're willing to work hard enough at it. How hard depends on where in the .pptx
package it's stored.
As a start, I'd recommend inspecting the XML of a slide that contains one of the controls you're thinking of using. The opc-diag
(Python) package is good for that job.
Getting a sense of the XML is quickest when you start as small as possible, for example by inspecting slide1.xml
of a presentation with a single slide containing a single shape, say a rectangle with the text "foobar" (which makes it easy to find in the XML with search).
Once you've worked out what the element(s) you're after look like, you can use XPath to find them and lxml
calls to read their values. So something that looks roughly like:
from pptx import Presentation
prs = Presentation("my-pptx.pptx")
sld = prs.slides[0]._element # ---XML element of slide---
aliases = sld.xpath(".//p:stdPr/p:alias")
print(aliases[0]["val"])
You may need to learn more about lxml
than you really wanted to know to make it work. For example, namespaces are somewhat painful to deal with.

- 26,423
- 5
- 54
- 80