You need slides, and the machine that generates them does not have Office. Three
approaches work, and the differences only show up when someone opens the file in WPS
or edits a text box.
Why not just export images or PDF
The most common shortcut is rendering slides to PNG and wrapping them in a deck. It
looks fine in a screenshot and fails immediately in use: the recipient cannot edit a
single word, and the first request in any real handoff is "can you change the title".
A .pptx is a zip of XML parts. Anything that produces a real one keeps the deck
editable, and that is the only property worth optimising for first.
Approach 1: python-pptx (start here)
from pptx import Presentation
from pptx.util import Inches
prs = Presentation() # default template is 4:3 — see pitfalls
prs.slide_width = Inches(13.333) # 16:9
prs.slide_height = Inches(7.5)
slide = prs.slides.add_slide(prs.slide_layouts[1]) # Title and Content
slide.shapes.title.text = "Q3 review"
frame = slide.placeholders[1].text_frame
frame.text = "Revenue up 12%"
bullet = frame.add_paragraph()
bullet.text = "Churn down to 1.8%"
bullet.level = 1
prs.save("out.pptx")
That is a valid, editable deck. python-pptx handles the relationship graph, content
types and layout inheritance for you, and it is the right answer for 90% of cases —
including charts, tables and images.
The reason to keep looking is styling: you inherit whatever the template gives you,
and fine-grained control means dropping into the XML anyway.
Approach 2: template substitution (best for branded decks)
If the deck must match a company template, do not rebuild it — open it and replace text.
from pptx import Presentation
prs = Presentation("brand-template.pptx")
for slide in prs.slides:
for shape in slide.shapes:
if shape.has_text_frame and "{{TITLE}}" in shape.text_frame.text:
shape.text_frame.text = shape.text_frame.text.replace("{{TITLE}}", title)
prs.save("out.pptx")
This keeps fonts, colours, master slides and footers exactly as designed, and it is
the approach I would pick for anything customer-facing. The cost is that every layout
you need has to exist in the template first.
Approach 3: raw OOXML
Writing the zip yourself is worth it when you need deterministic output (no library
version drift), or you are already generating layout geometry from a data model.
A minimal deck that PowerPoint will open needs:
[Content_Types].xml
_rels/.rels
ppt/presentation.xml
ppt/_rels/presentation.xml.rels
ppt/slideMasters/slideMaster1.xml (+ .rels)
ppt/slideLayouts/slideLayout1.xml (+ .rels)
ppt/slides/slide1.xml (+ .rels)
ppt/theme/theme1.xml
Miss the master or the theme and you get a repair prompt, not an error message. That
is the whole difficulty of this route: failures are silent until a human opens the file.
Choosing
| Situation | Use |
|---|---|
| One-off reports, charts, tables | python-pptx |
| Must match a corporate template | template substitution |
| Thousands of decks, stable output required | raw OOXML |
| Need pixel-perfect typography control | raw OOXML, or accept the template's |
Six things that actually break
-
The default template is 4:3.
Presentation()gives you 10x7.5 inches. Set bothslide_widthandslide_heightbefore adding slides. -
slide.placeholders[1]is the placeholderidx, not its position in the shapes list. Templates reuseidxacross layouts, so hard-coded indices break when the layout changes. -
Theme colours are references, not values. A slide stores
schemeClr val="accent1", not#2F5597. Changing a theme means changing the palette, not rewriting every shape. -
Setting
font.namedoes not touch CJK text. That writes<a:latin>. Chinese and Japanese need<a:ea>as well:
from lxml import etree
from pptx.oxml.ns import qn
def set_east_asian_font(run, typeface: str) -> None:
rPr = run.font._rPr
ea = rPr.find(qn("a:ea"))
if ea is None:
ea = etree.SubElement(rPr, qn("a:ea"))
ea.set("typeface", typeface)
-
Images must be embedded and registered.
add_picture()does both; if you write XML by hand, the image goes intoppt/media/and needs an entry in the slide's.relsplus a matching content type. Copying the file in is not enough. - Test in the three renderers people actually use. PowerPoint, WPS Office and LibreOffice disagree about spacing and font fallback. A deck that looks right in one can overflow in another — check the longest line you generate, not the average one.
The part I would steal
Generate the outline as structured data first, and treat slide rendering as a pure
function of that data. Text edits then cost milliseconds, while regeneration costs
seconds and can come back different in ways you did not ask for.
I build offline Windows tools, including one that does exactly this for decks — the
outline stays editable, the renderer never surprises you. Happy to compare notes on
OOXML layout inheritance if you have fought it too.
Top comments (0)