or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

charts.mdformatting.mdindex.mdpresentation.mdshapes.mdslides.mdtables.mdtext.mdutilities.md
tile.json

index.mddocs/

Python-PPTX

A 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.

Package Information

  • Package Name: python-pptx
  • Language: Python
  • Installation: pip install python-pptx

Core Imports

from pptx import Presentation

Common 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 RGBColor

Basic Usage

from 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')

Architecture

Python-PPTX follows a hierarchical object model that mirrors PowerPoint's document structure:

  • Presentation: Top-level container managing slides, masters, and document properties
  • Slides: Collection of individual slides with content and formatting
  • Shapes: Visual elements (text boxes, images, charts, tables) within slides
  • Text Framework: Rich text handling with runs, paragraphs, and character formatting
  • Drawing ML (DML): Low-level drawing and formatting capabilities

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.

Capabilities

Presentation Management

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."""

Presentation Management

Slide Operations

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): ...

Slide Operations

Shape Creation and Manipulation

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

Shapes

Text Formatting and Typography

Rich 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: bool

Text

Chart Creation and Data Visualization

Creating 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: CategoryAxis

Charts

Table Creation and Formatting

Creating 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): ...

Tables

Colors and Visual Formatting

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

Colors and Formatting

Measurement Units and Utilities

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

Utilities

Error Handling

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

  • File not found or permission issues when opening/saving presentations
  • Invalid image formats when adding pictures
  • Malformed chart data when creating visualizations
  • Unsupported .pptx features in older PowerPoint versions