python-pptx Documentation

repository·master·Indexed 25 days ago

https://github.com/scanny/python-pptx

A Python library for creating, reading, and updating PowerPoint 2007+ (.pptx) files without requiring the PowerPoint application. It provides APIs for managing slides, shapes, AutoShapes via MSO_SHAPE, and complex chart types including Category, XY, and Bubble charts. The library includes tools for manipulating drawing elements through ColorFormat, FillFormat, and LineFormat, as well as handling text sizing and connector types.

Tokens
91.2K
Snippets
231
Records
472
Agent score
85%

What's inside python-pptx

  1. Introduction to python-pptx

    master

    python-pptx is a Python library designed for creating and updating PowerPoint (.pptx) files. It is commonly used for:

    • Generating customized presentations from database content (e.g., for web applications).
    • Automating engineering status reports from work management systems.
    • Performing bulk updates to existing presentation libraries.
    • Automating the production of specific slides to avoid manual entry.
  2. Overview of python-pptx

    master

    python-pptx is a Python library designed for creating, reading, and updating PowerPoint (.pptx) files. It is useful for:

    • Generating presentations from dynamic content: Create slides from database queries, analytics output, or JSON payloads.
    • Automating slide production: Automate the creation of repetitive or tedious slides.
    • Analyzing PowerPoint files: Extract text and images from a corpus of PowerPoint files for tasks like search indexing.

    The library runs on any Python-capable platform (including macOS and Linux) and does not require the Microsoft PowerPoint application to be installed or licensed.

  3. Overview of python-pptx capabilities

    master

    python-pptx is an industrial-grade library designed for creating, reading, and updating PowerPoint (.pptx) files. It supports a wide range of Open XML presentation elements, allowing for robust automation in commercial settings.

    Key capabilities include:

    • Round-trip support: Read and save Open XML presentations (.pptx) while preserving elements.
    • Slide manipulation: Add new slides to a presentation.
    • Text handling: Populate text placeholders (e.g., bullet slides), add textboxes, and manipulate font properties like size and bold.
    • Media and Shapes: Add images at arbitrary positions/sizes, add auto shapes (polygons, flowcharts, etc.), and add tables.
    • Data Visualization: Add and manipulate column, bar, line, and pie charts.
    • Metadata: Access and change core document properties such as title and subject.
  4. Understand GroupShape recursion and XML structure

    master

    Grouping in PowerPoint is recursive: a GroupShape can contain other GroupShape objects.

    In the underlying XML, a group shape is represented by the <p:grpSp> element (type CT_GroupShape). The root shape tree of a slide (p:spTree) uses this same XML type, though it behaves slightly differently than a standard group shape.

  5. Understand the Text Hierarchy in PowerPoint

    master

    In PowerPoint, all text is contained within a shape. Only AutoShapes (which include text placeholders, text boxes, and geometric auto shapes) can directly contain text. These correspond to the <p:sp> XML element.

    The text hierarchy is structured as follows:

    1. TextFrame: Represented by a TextFrame object, every auto shape has one.
    2. Paragraph: A TextFrame contains one or more paragraphs.
    3. Elements: Each paragraph contains a sequence of zero or more elements, which can be:
      • Runs (<a:r>): Sequences of text.
      • Line Breaks (<a:br>).
      • Fields (<a:fld>).
  6. Understand Slide and Slide ID behavior

    master

    A slide is the primary visual container in a presentation, holding shape objects.

    Key concepts for developers:

    • Ownership: Slides are owned by the presentation object.
    • Slide ID: Each slide is assigned a unique integer identifier (starting at 256 and incrementing by 1).
    • ID Scope: The Slide ID is unique only within a single presentation. It is managed by the presentation part and is not recorded in the slide's own XML. Instead, it maps to a relationship ID in the presentation XML.
    • Persistence: Changing the order of slides does not change their IDs. Deleted slide IDs are not reused.
  7. Use ChartData objects to specify chart data

    master

    A ChartData object is used to specify the data depicted in a chart when creating a new chart or replacing data for an existing one. The specific subclass required depends on the chart type:

    • Category charts: Use CategoryChartData.
    • XY charts: Use XyChartData.
    • Bubble charts: Use BubbleChartData.
  8. Understand the placeholder architecture in python-pptx

    master

    Placeholders are pre-formatted containers that allow template designers to set visual styles while end-users focus on content. They function through a three-tier inheritance hierarchy:

    1. Master Placeholders: Exist on the Slide Master. They are 'inheritees' only.
    2. Layout Placeholders: Exist on the Slide Layout. They act as both 'inheritors' (from the Master) and 'inheritees' (to the Slide).
    3. Placeholder Shapes: Exist on the Slide. They are 'inheritors' only.

    Inheritance Rules:

    • Layouts inherit from Masters based on the placeholder type.
    • Slides inherit from Layouts based on the placeholder idx value.
    • Formatting: Position, size, fill, line, and font properties are inherited from the parent. Directly applied formatting on a slide placeholder overrides inherited values. To reset a placeholder to its template defaults, reapply the layout.
  9. Understand PowerPoint Chart Type mapping

    master

    PowerPoint supports 73 distinct chart types, which are implemented in python-pptx using 16 base XML elements (e.g., c:barChart, c:pie3DChart). The specific chart type used is determined by differentiating these base elements through child elements and attributes such as c:grouping, c:shape, c:barDir, or c:marker.

    When working with charts, you should be aware that a single XML element can represent multiple logical chart types depending on its configuration.

  10. Understand Base Slide characteristics

    master

    In python-pptx, Slide, SlideLayout, and SlideMaster objects all share common behaviors as they are all considered types of slides. The fundamental visual content container for a slide is the shape object.

    One shared attribute across these objects is the name (stored in the XML as p:sld/p:cSld/@name).

    Note on Slide Naming:

    • PowerPoint automatically populates names in SlideLayout objects.
    • In Slide and SlideMaster objects, the name attribute is often unpopulated.
    • If a slide has no explicit name, PowerPoint displays a default name like 'Slide {n}' (where n is the sequence number), but this default name is not written to the underlying XML attribute.
    • In the PowerPoint UI outline pane, the slide title is used instead of the name if a title exists.
  11. Understand AutoShape geometry and properties

    master

    In the MS Office API, an AutoShape (referred to as a shape in the PowerPoint UI) is a shape with preset geometry, such as a rectangle, circle, or star.

    An AutoShape consists of:

    • Type: A preset geometry (there are 187 pre-defined types).
    • Outline: A line style (can be None).
    • Fill: A fill color/pattern (can be None).
    • Effect: Visual effects (can be None).
    • Position and Size: Defined by its location and dimensions on the slide.

    In the underlying XML, each auto shape is contained within a <p:sp> element.

  12. Understand the text hierarchy in python-pptx

    master

    Text in PowerPoint is organized into a three-level hierarchy. To manipulate text, you must navigate through these levels:

    1. .Shape.text_frame: The container for all text in a shape. It controls margins, vertical alignment, wrapping, and auto-fit behavior.
    2. .TextFrame.paragraphs: A sequence of paragraphs within a text frame. Every text frame contains at least one paragraph.
    3. ._Paragraph.runs: The lowest level where actual text resides. Runs provide character-level formatting (font, size, color, bold, etc.).