Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Create PowerPoint Presentations with Python and python-pptx

Learn how to create PowerPoint presentations programmatically with python-pptx: add slides, text, shapes, images, and tables, then save editable .pptx files.

~4 min readPython
#python-pptx#PowerPoint automation#slide generation#office automation
Python code generating a PowerPoint slide with text and shapes, illustrating automated presentation creation

Creating PowerPoint presentations programmatically is straightforward with python-pptx. The library lets you build .pptx files entirely from Python code without opening the PowerPoint application. This makes it useful for generating decks from data, reports, or templates. This article shows how to build a presentation from scratch, add common elements, and handle practical concerns that come up when automating slide generation.

Installing python-pptx

Install the library with pip:

bash
pip install python-pptx

The library works on Windows, macOS, and Linux, and pip installs its required dependencies. After installation, import the main classes:

python
from pptx import Presentation from pptx.util import Inches, Pt from pptx.enum.text import PP_ALIGN from pptx.dml.color import RGBColor

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:

python
prs = Presentation() layout = prs.slide_layouts[1] # Title and Content layout slide = prs.slides.add_slide(layout)

In the default template, layout index 0 is a title slide, 1 is title and content, and 6 is a blank layout.

To set the slide title, access the placeholder:

python
slide.shapes.title.text = "Quarterly Revenue"

For content placeholders, you can access them by index:

python
body = slide.placeholders[1] body.text = "Total: $1.2M"

For a blank canvas, 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:

python
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:

python
tf.text = "First paragraph" p = tf.add_paragraph() p.text = "Second paragraph" p.level = 1 p.alignment = PP_ALIGN.CENTER run = p.runs[0] run.font.size = Pt(18) run.font.bold = True run.font.color.rgb = RGBColor(0x33, 0x66, 0x99)

RGBColor is available 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:

python
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:

python
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:

python
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():

python
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:

python
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 Presentation object to free memory.

The library is not designed for real-time slide editing; it is a file generator. For interactive manipulation, you would typically use 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 placeholders collection order is not guaranteed across templates.
  • Units are EMU (English Metric Units). The Inches and Pt helpers 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. Use RGBColor(0x33, 0x66, 0x99).
  • Text frames have auto-size off by default. Long text may overflow the box. If you need automatic resizing, import MSO_AUTO_SIZE from pptx.enum.text and set tf.auto_size = MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE.
  • Compatibility with older PowerPoint versions. python-pptx generates files in the modern .pptx format, which works with PowerPoint 2007 and later. It does not support the legacy .ppt format.

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.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.