DEV Community

Cover image for How to generate a PPTX file in Python without PowerPoint or Office installed
hipangwork
hipangwork

Posted on

How to generate a PPTX file in Python without PowerPoint or Office installed

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")
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

  1. The default template is 4:3. Presentation() gives you 10x7.5 inches. Set both slide_width and slide_height before adding slides.
  2. slide.placeholders[1] is the placeholder idx, not its position in the shapes list. Templates reuse idx across layouts, so hard-coded indices break when the layout changes.
  3. 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.
  4. Setting font.name does 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)
Enter fullscreen mode Exit fullscreen mode
  1. Images must be embedded and registered. add_picture() does both; if you write XML by hand, the image goes into ppt/media/ and needs an entry in the slide's .rels plus a matching content type. Copying the file in is not enough.
  2. 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)