Create PowerPoint Presentations with Python and python-pptx
python python pptx create powerpoint presentations: Learn how to create PowerPoint presentations programmatically with python-pptx: slides, text, shapes, images, table...
python python pptx create powerpoint presentations requires a clear understanding of the core syntax, runtime behavior, and practical implementation patterns demonstrated in the examples below.
When you need to generate PowerPoint decks from data, reports, or templates, python-pptx is the most direct library in the Python ecosystem. It lets you create .pptx files entirely from code, without touching the PowerPoint application. This article shows how to build a presentation from scratch, add common elements, and handle the practical concerns that come up when automating slide generation.
Installing python-pptx
Install the library with pip:
pip install python-pptx
The package depends on lxml and Pillow, so it works on Windows, macOS, and Linux. After installation, import the main classes:
from pptx import Presentation from pptx.util import Inches, Pt from pptx.enum.text import PP_ALIGN
Presentation is the core class representing a .pptx file. The utility modules provide measurement and formatting helpers.
Creating a Presentation and Adding Slides
A new presentation starts empty. You add slides using a layout from the default template:
prs = Presentation() layout = prs.slide_layouts[1] # Title and Content layout slide = prs.slides.add_slide(layout)
The default template has several layouts. Index 0 is usually a title slide, 1 is title and content, and 6 is a blank layout. For full control, use a blank layout and add text boxes manually.
To set the slide title, access the placeholder:
slide.shapes.title.text = "Quarterly Revenue"
For content placeholders, you can access them by index:
body = slide.placeholders[1] body.text = "Total: $1.2M"
If you need a completely custom layout, use slide_layouts[6] and add your own text boxes.
Adding Text and Formatting
Text boxes are the most flexible way to place text anywhere on a slide. Create one with add_textbox and specify position and size:
top = Inches(1) left = Inches(1) width = Inches(8) height = Inches(1.5) textbox = slide.shapes.add_textbox(left, top, width, height) tf = textbox.text_frame
Set the text and apply formatting to paragraphs and runs:
tf.text = "First paragraph" p = tf.add_paragraph() p.text = "Second paragraph" p.level = 1 run = p.runs[0] run.font.size = Pt(18) run.font.bold = True run.font.color.rgb = RGBColor(0x33, 0x66, 0x99)
RGBColor comes from pptx.dml.color. Each paragraph has a level property that controls indentation. Runs allow fine-grained formatting, so you can mix styles within a paragraph.
Working with Shapes, Images, and Tables
Beyond text, python-pptx supports autoshapes, pictures, and tables.
Adding Shapes
Use add_shape with an enum from pptx.enum.shapes.MSO_SHAPE:
from pptx.enum.shapes import MSO_SHAPE shape = slide.shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Inches(1), Inches(2), Inches(3), Inches(1)) shape.text = "Click here" shape.fill.solid() shape.fill.fore_color.rgb = RGBColor(0x00, 0x70, 0xC0)
Shapes can have text, fills, and outlines. They are useful for callouts, buttons, or simple diagrams.
Inserting Images
Add a picture from a file or a file-like object:
slide.shapes.add_picture("chart.png", Inches(0.5), Inches(2), width=Inches(6))
The image is embedded in the .pptx, so the source file is not needed later. If you omit width and height, the image keeps its native size.
Building Tables
Tables are created with add_table and then populated row by row:
rows, cols = 3, 2 table_shape = slide.shapes.add_table(rows, cols, Inches(1), Inches(3), Inches(8), Inches(2)) table = table_shape.table table.cell(0, 0).text = "Product" table.cell(0, 1).text = "Sales" table.cell(1, 0).text = "Widget" table.cell(1, 1).text = "$10k"
You can style cells by accessing their fill and text frame properties. Tables are rendered as native PowerPoint tables, so they remain editable in the final file.
Saving and Opening Presentations
After building the deck, save it with save():
prs.save("report.pptx")
The file is written in the standard Office Open XML format. You can also open an existing presentation and modify it:
prs = Presentation("template.pptx") # add slides, modify content prs.save("updated.pptx")
When opening a template, the existing layouts and theme are preserved. This is useful for generating reports that match a corporate design.
Performance and Memory Considerations for Large Decks
Creating a presentation with hundreds of slides is possible, but memory usage grows with the number of shapes and images. Each picture is stored in memory as a binary blob until the file is saved. For very large decks, consider the following:
- Reuse images by referencing the same file path; python-pptx deduplicates identical images internally.
- Avoid adding thousands of individual text boxes; use tables or bullet placeholders where possible.
- If you generate many presentations in a loop, save each one and release the
Presentationobject to free memory.
The library is not designed for real-time slide editing; it is a file generator. For interactive manipulation, you would need the PowerPoint COM API on Windows, but for batch generation python-pptx is efficient enough for typical reporting workloads.
Common Pitfalls and Compatibility Notes
Several issues trip up developers new to python-pptx:
- Placeholder indexes vary by layout. Always inspect the layout you are using. The
placeholderscollection order is not guaranteed across templates. - Units are EMU (English Metric Units). The
InchesandPthelpers convert to EMU. Mixing raw integers without conversion leads to misplaced elements. - Font colors require
RGBColor. Passing a string like"#336699"will raise an error. UseRGBColor(0x33, 0x66, 0x99). - Text frames have auto-size off by default. Long text may overflow the box. Set
tf.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPEif you need automatic resizing. - Compatibility with older PowerPoint versions. python-pptx generates files in the modern
.pptxformat, which works with PowerPoint 2007 and later. It does not support the legacy.pptformat.
When you need to create PowerPoint presentations with Python, python-pptx gives you a reliable, scriptable path. The library's API is consistent, and the generated files behave like any native PowerPoint document. Start with a blank presentation, add slides and content, and save. For repetitive reporting tasks, this approach eliminates manual copy-paste and ensures every deck follows the same structure.