python-pptx

raw JSON →
1.0.2 verified Tue May 12 auth: no python install: verified

python-pptx is a Python library for creating, reading, and updating PowerPoint (.pptx) files. A typical use would be generating a PowerPoint presentation from dynamic content such as a database query, analytics output, or a JSON payload. It is currently at version 1.0.2 and aims for industrial-grade reliability with a comprehensive testing regimen.

pip install python-pptx
error ModuleNotFoundError: No module named 'pptx'
cause The python-pptx library is installed via `pip install python-pptx` but its core package name for importing is `pptx`.
fix
Use import pptx or from pptx import Presentation to import the library.
error AttributeError: 'Presentation' object has no attribute 'add_slide'
cause The `add_slide` method is called directly on the `Presentation` object instead of its `slides` collection.
fix
Access the add_slide method through the slides collection: slide_layout = prs.slide_layouts[0] slide = prs.slides.add_slide(slide_layout)
error KeyError: 'chart_type'
cause An invalid or misspelled chart type is provided when attempting to add a chart to a slide.
fix
Use a valid builtin_chart_type enum member from pptx.enum.chart, for example: XL_CHART_TYPE.COLUMN_CLUSTERED.
error FileNotFoundError: [Errno 2] No such file or directory: 'template.pptx'
cause The specified path to the presentation file (for loading or saving) is incorrect, or the file does not exist at that location.
fix
Ensure the file exists and the path (absolute or relative) is correct, for example: prs = Presentation('path/to/your/template.pptx').
breaking Several API changes occurred around versions 0.6.x to 1.0.x, including enumeration renames (e.g., `pptx.enum.MSO_COLOR_TYPE` moved to `pptx.enum.dml.MSO_COLOR_TYPE`) and property renames (e.g., `SlideLayout.slidemaster` to `SlideLayout.slide_master`). Additionally, line-break encoding in `.text` properties changed from newline (\n) to vertical-tab (\v) in version 0.6.18 to better distinguish from paragraph boundaries.
fix Consult the release notes and official documentation for specific migration paths. Update code to use new enumeration paths and property names. Handle `\v` for line breaks when parsing or generating text.
gotcha The library exclusively supports PowerPoint Open XML files (`.pptx` format, introduced with PowerPoint 2007). Older PowerPoint `.ppt` files (PowerPoint 2003 and earlier) are not supported.
fix Ensure all input files are in the `.pptx` format. Convert older `.ppt` files using Microsoft PowerPoint if necessary.
gotcha When saving a presentation, if you specify a filename that already exists, `python-pptx` will overwrite the file silently without any warning or prompt.
fix Implement explicit checks or user confirmation mechanisms in your application before saving to an existing file path, or ensure unique filenames are generated.
gotcha Avoid directly instantiating internal classes (e.g., `pptx.presentation.Presentation`). Instead, use the `pptx.Presentation()` factory function to create a new presentation or load an existing one.
fix Always use `prs = Presentation()` or `prs = Presentation('path/to/file.pptx')`.
gotcha While `pip install python-pptx` typically handles core dependencies, if you use the `setup.py` installation method, or encounter issues with charting, verify that `XlsxWriter` is installed, as it is required for charting features.
fix If charting features are used and issues arise, explicitly install `XlsxWriter`: `pip install XlsxWriter`.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.33s 53.7M
3.10 alpine (musl) - - 0.36s 53.7M
3.10 slim (glibc) wheel 3.1s 0.25s 55M
3.10 slim (glibc) - - 0.24s 55M
3.11 alpine (musl) wheel - 0.52s 56.9M
3.11 alpine (musl) - - 0.80s 56.9M
3.11 slim (glibc) wheel 2.8s 0.47s 58M
3.11 slim (glibc) - - 0.48s 58M
3.12 alpine (musl) wheel - 0.39s 48.6M
3.12 alpine (musl) - - 0.51s 48.6M
3.12 slim (glibc) wheel 2.6s 0.42s 50M
3.12 slim (glibc) - - 0.44s 50M
3.13 alpine (musl) wheel - 0.39s 48.3M
3.13 alpine (musl) - - 0.43s 48.2M
3.13 slim (glibc) wheel 2.6s 0.39s 49M
3.13 slim (glibc) - - 0.43s 49M
3.9 alpine (musl) wheel - 0.32s 51.1M
3.9 alpine (musl) - - 0.36s 51.1M
3.9 slim (glibc) wheel 3.6s 0.33s 52M
3.9 slim (glibc) - - 0.32s 52M

This example demonstrates how to create a new PowerPoint presentation, add a title slide with text, and then add a blank slide with a custom textbox.

from pptx import Presentation
from pptx.util import Inches

# Create a new presentation
prs = Presentation()

# Add a title slide (layout index 0)
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)

# Set title and subtitle
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Hello, World!"
subtitle.text = "python-pptx was here!"

# Add a blank slide (layout index 6) and add a textbox
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = width = height = Inches(1.0)
textbox = slide.shapes.add_textbox(left, top, width, height)
textbox.text = "This is a custom textbox."

# Save the presentation
prs.save('test.pptx')
print("Presentation 'test.pptx' created successfully.")