Fundamentals 14 min read

Master PowerPoint Automation with python-pptx: A Complete Guide

This tutorial walks you through using the python-pptx library to read, modify, and generate PowerPoint files—including installing the package, extracting slides, shapes, and text, and programmatically adding text boxes, images, tables, and custom styling—providing ready‑to‑run code snippets for each step.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master PowerPoint Automation with python-pptx: A Complete Guide

Introduction

The article introduces a comprehensive Python automation manual that focuses on manipulating PowerPoint presentations with the python-pptx library, covering both reading existing PPT files and creating new content programmatically.

python-pptx Module Overview

The python-pptx package enables creation and modification of .pptx files. It is not part of the Python standard library and must be installed separately.

Installation and Import

pip install python-pptx
import pptx

Reading PPT Content

Getting Slides

from pptx import Presentation
prs = Presentation("example.pptx")
for slide in prs.slides:
    print(slide)

Getting Shapes and Text

for slide in prs.slides:
    for shape in slide.shapes:
        if shape.has_text_frame:
            print(shape.text_frame.text)

Accessing Specific Slide and Paragraphs

for i, slide in enumerate(prs.slides):
    if i == 5:
        for shape in slide.shapes:
            if shape.has_text_frame:
                for paragraph in shape.text_frame.paragraphs:
                    print(paragraph.text)

Writing to PPT

Using Slide Layouts and Placeholders

prs = Presentation("blank.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])
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}"
prs.save("output.pptx")

Adding a Text Box

from pptx.util import Cm
left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
tf.text = "This is a sample text box"

Adding a Picture

pic = slide.shapes.add_picture("sample.png", left, top)

Adding a Table

rows, cols = 5, 3
left = top = Cm(5)
width = Cm(18)
height = Cm(3)
table = slide.shapes.add_table(rows, cols, left, top, width, height).table
# Example data filling
data = [
    ["Name", "Gender", "Score"],
    ["Zhang San", "M", "96"],
    ["Li Si", "F", "87"],
    ["Wang Wu", "F", "90"],
    ["Zhao Liu", "M", "78"]
]
for r in range(rows):
    for c in range(cols):
        table.cell(r, c).text = str(data[r][c])
prs.save("table_example.pptx")

Adjusting Text Box Style

Position, Background Color, Border, Alignment, Font

from pptx.enum.text import MSO_ANCHOR
from pptx.dml.color import RGBColor
from pptx.util import Pt
# Vertical alignment and word wrap
tf.vertical_anchor = MSO_ANCHOR.BOTTOM
tf.word_wrap = True
# Background fill
fill = text_box.fill
fill.solid()
fill.fore_color.rgb = RGBColor(247, 150, 70)
# Border line
line = text_box.line
line.color.rgb = RGBColor(255, 0, 0)
line.width = Cm(0.3)
# Paragraph formatting
p = tf.add_paragraph()
p.text = "Styled paragraph"
p.font.bold = True
p.font.name = "SimSun"
p.font.size = Pt(30)
p.font.color.rgb = RGBColor(247, 150, 70)
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

scriptingpowerpointpresentationPPTX
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

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.