Create, read, and update PowerPoint 2007+ (.pptx) files programmatically
npx @tessl/cli install tessl/pypi-python-pptx@1.0.0A comprehensive Python library for creating, reading, and updating Microsoft PowerPoint 2007+ (.pptx) files programmatically. Python-PPTX enables developers to generate dynamic presentations from data sources like databases, APIs, or analytics outputs without requiring PowerPoint installation.
pip install python-pptxfrom pptx import PresentationCommon patterns for working with specific components:
from pptx.util import Inches, Cm, Pt
from pptx.enum.text import PP_PARAGRAPH_ALIGNMENT
from pptx.enum.shapes import MSO_SHAPE
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE
from pptx.dml.color import RGBColorfrom pptx import Presentation
from pptx.util import Inches
from pptx.enum.shapes import MSO_SHAPE
# Create a new presentation
prs = Presentation()
# Add a slide with title and content layout
slide_layout = prs.slide_layouts[1] # Title and Content layout
slide = prs.slides.add_slide(slide_layout)
# Add title and content
title = slide.shapes.title
title.text = "My Presentation Title"
content = slide.placeholders[1] # Content placeholder
tf = content.text_frame
tf.text = "This is the first bullet point"
# Add another bullet point
p = tf.add_paragraph()
p.text = "This is the second bullet point"
p.level = 1
# Add a shape
left = top = width = height = Inches(1)
shape = slide.shapes.add_shape(
MSO_SHAPE.RECTANGLE, left, top, width, height
)
shape.text = "This is a text box"
# Save the presentation
prs.save('my-presentation.pptx')Python-PPTX follows a hierarchical object model that mirrors PowerPoint's document structure:
The library is built on Open Packaging Conventions (OPC) and Office Open XML standards, providing robust XML parsing and generation capabilities for reliable .pptx file handling.
Core functionality for creating, opening, and saving PowerPoint presentations. Includes slide management, master templates, and document properties.
from typing import IO
from pptx import presentation
def Presentation(pptx: str | IO[bytes] | None = None) -> presentation.Presentation:
"""
Create or load a presentation.
Parameters:
- pptx: str | IO[bytes] | None, path to .pptx file, file-like object, or None for new presentation
Returns:
presentation.Presentation object
"""
class Presentation:
"""Main presentation object containing slides and configuration."""
slides: Slides
slide_layouts: SlideLayouts
slide_masters: SlideMasters
slide_width: int # in EMU
slide_height: int # in EMU
core_properties: CoreProperties
def save(self, file):
"""Save presentation to file path or file-like object."""Creating, accessing, and managing slides within presentations. Includes slide layouts, masters, and slide-specific properties.
class Slides:
"""Collection of slides in a presentation."""
def add_slide(self, slide_layout): ...
def __getitem__(self, index): ...
def __len__(self): ...
class Slide:
"""Individual slide with shapes and properties."""
shapes: SlideShapes
slide_layout: SlideLayout
notes_slide: NotesSlide
@property
def name(self): ...Adding and configuring visual elements including text boxes, images, autoshapes, and connectors. Covers shape properties, positioning, and styling.
class SlideShapes:
"""Collection of shapes on a slide."""
def add_textbox(self, left, top, width, height): ...
def add_picture(self, image_file, left, top, width=None, height=None): ...
def add_shape(self, autoshape_type, left, top, width, height): ...
def add_table(self, rows, cols, left, top, width, height): ...
def add_chart(self, chart_type, x, y, cx, cy, chart_data): ...
class BaseShape:
"""Base class for all shapes."""
name: str
shape_type: MSO_SHAPE_TYPE
left: int # position in EMU
top: int
width: int
height: intRich text handling including paragraphs, runs, fonts, colors, and alignment. Supports advanced typography features and text frame management.
class TextFrame:
"""Container for text content."""
paragraphs: list
text: str
def add_paragraph(self): ...
class Font:
"""Text formatting properties."""
name: str
size: Pt
bold: bool
italic: bool
color: ColorFormat
underline: boolCreating and configuring charts including bar, line, pie, scatter, and bubble charts. Includes data binding, formatting, and chart element customization.
class CategoryChartData:
"""Data container for category-based charts."""
def add_series(self, name, values=None): ...
class Chart:
"""Chart object with plot area, legend, and axes."""
chart_type: XL_CHART_TYPE
plots: list
legend: Legend
value_axis: ValueAxis
category_axis: CategoryAxisCreating and styling tables with cells, rows, and columns. Includes cell merging, text formatting, and table-wide styling options.
class Table:
"""Table with rows and columns."""
rows: _RowCollection
columns: _ColumnCollection
def cell(self, row_idx, col_idx): ...
class _Cell:
"""Individual table cell."""
text: str
text_frame: TextFrame
fill: FillFormat
def merge(self, other_cell): ...Color specification and visual formatting including fills, lines, shadows, and effects. Supports RGB, theme colors, and transparency.
class RGBColor:
"""RGB color specification."""
def __init__(self, r, g, b): ...
class ColorFormat:
"""Color formatting with type-specific properties."""
rgb: RGBColor
theme_color: MSO_THEME_COLOR_INDEX
brightness: float
class FillFormat:
"""Fill formatting for shapes and text."""
solid(): ...
gradient(): ...
patterned(): ...Length measurements, positioning utilities, and helper functions for working with PowerPoint's coordinate system and measurements.
def Inches(inches):
"""Create Length from inches measurement."""
def Cm(cm):
"""Create Length from centimeters measurement."""
def Pt(points):
"""Create Length from points measurement."""
def Mm(mm):
"""Create Length from millimeters measurement."""
class Length:
"""Length measurement with unit conversion."""
inches: float
cm: float
pt: float
mm: floatclass PythonPptxError(Exception):
"""Base exception for python-pptx operations."""
class PackageNotFoundError(PythonPptxError):
"""Raised when .pptx file cannot be found."""
class InvalidXmlError(PythonPptxError):
"""Raised when XML content is invalid."""Common error scenarios: