
python-pptx is a Python library for PowerPoint files. It opens a .pptx (or a blank one), and gives you slides, layouts, shapes, text frames, pictures, tables, charts and media as Python objects.
Steve Canny wrote it, the same author as python-docx. MIT licence, Python 3.8+. The latest release is 1.0.2, from 7th August 2024. It's stable and slow-moving: plan for the features it has today.
What it does well
- Slides from a template. Open a deck that holds your masters and layouts, add slides on those layouts, fill the placeholders.
- Shapes with exact geometry: text boxes, rectangles, lines, pictures, positioned in EMU, inches or cm.
- Native charts (bar, line, pie, scatter) from data, which stay editable in PowerPoint.
- Tables, with cell fills and merges.
- Read and patch an existing deck: walk the shapes, change text, replace an image.
- Escape hatch: every object exposes its XML element (
shape._element), so whatever the API lacks you can still write with lxml.
Install and use
pip install python-pptx # the import name is pptx
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation("template.pptx") # or Presentation() for a blank 4:3 deck
slide = prs.slides.add_slide(prs.slide_layouts[5]) # "Title Only" in the default template
slide.shapes.title.text = "Next steps"
box = slide.shapes.add_textbox(Inches(0.8), Inches(1.6), Inches(8), Inches(3))
tf = box.text_frame
tf.word_wrap = True
tf.text = "1. Agree the pilot scope"
p = tf.add_paragraph()
p.text = "2. Schedule the kick-off"
p.font.size = Pt(20)
slide.shapes.add_picture("diagram.png", Inches(0.8), Inches(4.8), height=Inches(2))
prs.save("next-steps.pptx")
For 16:9, set prs.slide_width = Inches(13.333) and prs.slide_height = Inches(7.5) on a blank deck, or start from a 16:9 template.
Limits
- No rendering. It can't show you a slide or make a PDF or PNG. Render with PowerPoint, LibreOffice or another tool to check the result.
- No auto-fit at build time. Text that overflows its box stays overflowed until PowerPoint recalculates it. Measure or cap your text yourself.
- No animations or transitions in the API, and only basic media support.
- Fonts aren't embedded. The deck names a font, and a machine without it substitutes another.
- Slide copying between decks isn't supported; you rebuild the slide.
Gotchas I hit
⚠️ WARNING:
add_moviewritesr:id=""on the video's click hyperlink. An empty relationship id is invalid. PowerPoint for Mac opens the deck, plays the video, then refuses to save it ("The document could not be saved"). Windows is more forgiving, so the bug ships unnoticed. Delete the empty attribute after insertion;action="ppaction://media"works on its own.
from pptx.oxml.ns import qn
def strip_empty_rids(slide) -> None:
"""Remove the empty r:id python-pptx leaves on media hyperlinks."""
for el in slide._element.iter(qn("a:hlinkClick")):
if el.get(qn("r:id")) == "":
del el.attrib[qn("r:id")]
⚠️ WARNING:
add_moviemakes videos click-to-play with no loop. For a video that starts by itself and loops, patch the slide's timing XML: set everyp:condwithdelay="indefinite"todelay="0", and addrepeatCount="indefinite"to thep:cTnof eachp:cMediaNode.
- LibreOffice shows ghosts. A LibreOffice render of the deck draws empty layout placeholders that PowerPoint hides. Don't "fix" them; render with a tool that matches PowerPoint instead.
- Check a repaired package by round-tripping it through LibreOffice (
soffice --headless --convert-to pptx): it succeeds only on a well-formed file.
How I use it in NicAI
My decks are written as HTML on reveal.js first (see reveal.js and How I build decks with AI: HTML and PowerPoint from 1 plan). When a deck has to go out as PowerPoint, python-pptx builds it:
- Composed from the finished HTML. A converter reads each slide and turns it into a spec: template, title, bullets, images, video. Then 1 python-pptx function per slide template (I call them archetypes: title, divider, statement, split image, card grid, stat band, comparison table, quote, process flow, closing) redraws it.
- Native, not screenshots. Real text boxes, real shapes, embedded video that plays and loops. A slide made of screenshots looks right and nobody can edit it.
- A design kit module holds the brand constants (colours, fonts, the 16:9 size) and the primitives: slide, text, rectangle, picture fitted with
containorcover, logo, footer, and the video fixes above. - The PowerPoint becomes the source once a human edits it. The converter refuses to overwrite a
.pptxnewer than its HTML. Small changes go into the.pptxitself through find-and-replace that survives moved shapes. A structural change is a rebuild, and I'm told which edits it drops. - Diagrams too. My architecture diagrams export to an editable slide the same way.
QA: the agent renders every slide to PNG with DeckCraft, a headless PowerPoint clone (see WordCraft), reads the images, and checks text overflow and the video settings. A scan of the package for empty r:id values runs on every build.
Further reading

