Fundamentals 11 min read

Manipulating PowerPoint Files with python-pptx: Installation, Basic Operations, and Advanced Examples

This tutorial explains how to install the python-pptx library, outlines the basic PPT object model, demonstrates extracting text and shapes, shows how to create and modify slides, work with placeholders, and provides a complete example that generates personalized certificates from Excel data, all using Python.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Manipulating PowerPoint Files with python-pptx: Installation, Basic Operations, and Advanced Examples

Installation

Install the python-pptx package using pip. Windows users can run pip install python-pptx , macOS users can run pip3 install python-pptx , and a Chinese mirror can be used if needed.

<code>pip install -i https://pypi.tuna.tsinghua.edu.cn/simple python-pptx</code>

Importing the Module

After installation, import the library with from pptx import Presentation .

PPT Basic Structure

The library represents a presentation as a hierarchy of objects: Presentation (the whole file), Slide (each page), Shape (text boxes, images, etc.), Run (short text fragments), and Paragraph (a collection of runs).

Getting PPT Content

Open a file and iterate over its slides and shapes to read text:

<code>from pptx import Presentation
prs = Presentation("demo.pptx")
for slide in prs.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            print(shape.text)
        print("--------------------------")
</code>

To access paragraphs within a shape:

<code>for paragraph in shape.text_frame.paragraphs:
    print(paragraph.text)
    print('----------paragraph------------')
</code>

Creating and Writing to Slides

Use prs.slides.add_slide(prs.slide_layouts[index]) to add a new slide with a specific layout. The library provides nine built‑in layouts (Title, Title and Content, Section Header, etc.).

Set text directly on a shape or placeholder:

<code>slide = prs.slides.add_slide(prs.slide_layouts[1])
title_shape = slide.shapes.title
title_shape.text = "My Python Presentation"
subtitle = slide.shapes.placeholders[1]
subtitle.text = "Subtitle text"
new_par = subtitle.text_frame.add_paragraph()
new_par.text = "Additional paragraph"
new_par.level = 1
</code>

Working with Placeholders

Iterate over slide.placeholders to discover their index, name, and type, then assign text:

<code>for shape in slide.placeholders:
    phf = shape.placeholder_format
    print(f"{phf.idx}--{shape.name}--{phf.type}")
    shape.text = f"{phf.idx}--{shape.name}--{phf.type}"
</code>

Adding Shapes and Text Boxes

On a blank layout you can add a textbox or a shape such as a rounded rectangle:

<code>from pptx.util import Cm, Pt, Inches
from pptx.enum.shapes import MSO_SHAPE
slide = prs.slides.add_slide(prs.slide_layouts[6])
left = top = width = height = Cm(3)
textbox = slide.shapes.add_textbox(left, top, width, height)
textbox.text_frame.text = "Welcome to the Python PPT tutorial"
paragraph = textbox.text_frame.add_paragraph()
paragraph.text = "More detailed description"
paragraph.font.bold = True
paragraph.font.size = Pt(15)
shape = slide.shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Inches(1), Inches(1), Inches(2), Inches(1))
</code>

Comprehensive Example – Generating Certificates

The script reads student names and donation counts from an Excel file using openpyxl , opens a PPT template, replaces placeholder text (e.g., "__" for name, "__册" for book count, and a date placeholder), adjusts font sizes, and saves a personalized PPT for each student.

<code>from pptx import Presentation
from openpyxl import load_workbook
from datetime import datetime
wb = load_workbook('students.xlsx')
sheet = wb['Sheet1']
for row in sheet.iter_rows(min_row=2, max_row=8, min_col=1, max_col=2):
    prs = Presentation('template.pptx')
    for slide in prs.slides:
        for shape in slide.shapes:
            if shape.has_text_frame:
                for paragraph in shape.text_frame.paragraphs:
                    if paragraph.text == '__':
                        paragraph.text = row[0].value
                    if paragraph.text == '__册':
                        paragraph.text = f"{row[1].value}册"
                    if paragraph.text == '日期:':
                        paragraph.text = datetime.now().strftime('%Y年%m月%d日')
    prs.save(f"certificate_{row[0].value}.pptx")
</code>

Conclusion

The python-pptx library provides a powerful, pure‑Python way to automate the creation, reading, and modification of PowerPoint files, making it suitable for batch document generation, data‑driven presentations, and custom slide manipulation.

Pythondocument generationpython-pptxPowerPoint automationexcel integration
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.