python-pptx
Create and manipulate PowerPoint presentations programmatically. Build slide decks with layouts, shapes, charts, tables, and images. Generate data-driven presentations from templates.
What this skill does
# Python-pptx PowerPoint Automation Skill
## Overview
Python-pptx is a Python library for creating and updating PowerPoint (.pptx) presentations. This skill covers comprehensive patterns for presentation automation including:
- **Presentation creation** with multiple slide layouts
- **Shape manipulation** including text boxes, images, and geometric shapes
- **Chart generation** for data visualization within slides
- **Table creation** for structured data display
- **Master slide customization** for branding consistency
- **Template-based generation** for consistent presentations
- **Placeholder management** for dynamic content insertion
## When to Use This Skill
### USE when:
- Generating presentations from data automatically
- Creating standardized report presentations
- Building slide decks with consistent branding
- Automating dashboard presentations
- Creating training materials from templates
- Generating client presentations from databases
- Building presentation pipelines for regular reports
- Creating slides with charts and tables from data
- Mass-producing presentations with variable content
### DON'T USE when:
- Need real-time presentation editing (use PowerPoint)
- Creating presentations with complex animations
- Need advanced transitions (limited support)
- Require embedded videos with playback controls
- Need to preserve complex PowerPoint features
- Creating presentations from scratch without Python (use PowerPoint)
## Prerequisites
### Installation
```bash
# Basic installation
pip install python-pptx
# Using uv (recommended)
uv pip install python-pptx
# With image support
pip install python-pptx Pillow
# Full installation for charts
pip install python-pptx Pillow lxml
```
### Verify Installation
```python
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.text import PP_ALIGN
print("python-pptx installed successfully!")
```
## Core Capabilities
### 1. Basic Presentation Creation
```python
"""
Create a basic PowerPoint presentation with common slide types.
"""
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.enum.text import PP_ALIGN, MSO_ANCHOR
from pptx.dml.color import RgbColor
from pptx.enum.shapes import MSO_SHAPE
def create_basic_presentation(output_path: str) -> None:
"""Create a basic presentation with various slide types."""
# Create presentation
prs = Presentation()
# Set slide dimensions (16:9 widescreen)
prs.slide_width = Inches(13.333)
prs.slide_height = Inches(7.5)
# Slide 1: Title Slide
title_layout = prs.slide_layouts[0] # Title slide layout
slide1 = prs.slides.add_slide(title_layout)
title = slide1.shapes.title
subtitle = slide1.placeholders[1]
title.text = "Q1 2026 Business Review"
subtitle.text = "Strategic Planning and Performance Analysis"
# Format title
for paragraph in title.text_frame.paragraphs:
paragraph.font.size = Pt(44)
paragraph.font.bold = True
# Slide 2: Title and Content
bullet_layout = prs.slide_layouts[1] # Title and content
slide2 = prs.slides.add_slide(bullet_layout)
title2 = slide2.shapes.title
body2 = slide2.placeholders[1]
title2.text = "Key Highlights"
tf = body2.text_frame
tf.text = "Revenue grew 15% year-over-year"
p1 = tf.add_paragraph()
p1.text = "Customer satisfaction reached 92%"
p1.level = 0
p2 = tf.add_paragraph()
p2.text = "Expanded to 3 new markets"
p2.level = 0
p3 = tf.add_paragraph()
p3.text = "North America"
p3.level = 1
p4 = tf.add_paragraph()
p4.text = "Europe"
p4.level = 1
p5 = tf.add_paragraph()
p5.text = "Asia Pacific"
p5.level = 1
# Slide 3: Two Content Slide
two_content_layout = prs.slide_layouts[3] # Two content
slide3 = prs.slides.add_slide(two_content_layout)
title3 = slide3.shapes.title
title3.text = "Comparison Overview"
# Left content
left_placeholder = slide3.placeholders[1]
tf_left = left_placeholder.text_frame
tf_left.text = "Before"
p = tf_left.add_paragraph()
p.text = "Manual processes"
p = tf_left.add_paragraph()
p.text = "Limited scalability"
p = tf_left.add_paragraph()
p.text = "Higher costs"
# Right content
right_placeholder = slide3.placeholders[2]
tf_right = right_placeholder.text_frame
tf_right.text = "After"
p = tf_right.add_paragraph()
p.text = "Automated workflows"
p = tf_right.add_paragraph()
p.text = "Unlimited scale"
p = tf_right.add_paragraph()
p.text = "Cost reduction"
# Slide 4: Section Header
section_layout = prs.slide_layouts[2] # Section header
slide4 = prs.slides.add_slide(section_layout)
title4 = slide4.shapes.title
title4.text = "Financial Overview"
# Slide 5: Blank slide with custom shapes
blank_layout = prs.slide_layouts[6] # Blank
slide5 = prs.slides.add_slide(blank_layout)
# Add custom text box
left = Inches(0.5)
top = Inches(0.5)
width = Inches(12)
height = Inches(1)
textbox = slide5.shapes.add_textbox(left, top, width, height)
tf = textbox.text_frame
p = tf.paragraphs[0]
p.text = "Custom Content Slide"
p.font.size = Pt(32)
p.font.bold = True
p.alignment = PP_ALIGN.CENTER
# Add shapes
shapes = slide5.shapes
# Rectangle
rect = shapes.add_shape(
MSO_SHAPE.RECTANGLE,
Inches(1), Inches(2),
Inches(3), Inches(2)
)
rect.fill.solid()
rect.fill.fore_color.rgb = RgbColor(0x44, 0x72, 0xC4)
rect.text = "Feature A"
# Rounded rectangle
rounded = shapes.add_shape(
MSO_SHAPE.ROUNDED_RECTANGLE,
Inches(5), Inches(2),
Inches(3), Inches(2)
)
rounded.fill.solid()
rounded.fill.fore_color.rgb = RgbColor(0x70, 0xAD, 0x47)
rounded.text = "Feature B"
# Oval
oval = shapes.add_shape(
MSO_SHAPE.OVAL,
Inches(9), Inches(2),
Inches(3), Inches(2)
)
oval.fill.solid()
oval.fill.fore_color.rgb = RgbColor(0xED, 0x7D, 0x31)
oval.text = "Feature C"
# Save presentation
prs.save(output_path)
print(f"Presentation saved to {output_path}")
create_basic_presentation("basic_presentation.pptx")
```
### 2. Advanced Text Formatting
```python
"""
Advanced text formatting with runs, fonts, and paragraph styles.
"""
from pptx import Presentation
from pptx.util import Inches, Pt, Emu
from pptx.enum.text import PP_ALIGN, MSO_ANCHOR
from pptx.dml.color import RgbColor
from pptx.oxml.ns import nsmap
def create_formatted_presentation(output_path: str) -> None:
"""Create presentation with advanced text formatting."""
prs = Presentation()
# Slide with formatted text
slide_layout = prs.slide_layouts[6] # Blank
slide = prs.slides.add_slide(slide_layout)
# Title with formatting
title_box = slide.shapes.add_textbox(
Inches(0.5), Inches(0.3),
Inches(12), Inches(1)
)
tf = title_box.text_frame
p = tf.paragraphs[0]
p.alignment = PP_ALIGN.CENTER
# Multiple runs with different formatting
run1 = p.add_run()
run1.text = "Quarterly "
run1.font.size = Pt(40)
run1.font.bold = True
run1.font.color.rgb = RgbColor(0x2F, 0x54, 0x96)
run2 = p.add_run()
run2.text = "Performance"
run2.font.size = Pt(40)
run2.font.bold = True
run2.font.color.rgb = RgbColor(0x70, 0xAD, 0x47)
run3 = p.add_run()
run3.text = " Report"
run3.font.size = Pt(40)
run3.font.bold = True
run3.font.color.rgb = RgbColor(0x2F, 0x54, 0x96)
# Formatted paragraph with various styles
content_box = slide.shapes.add_textbox(
Inches(0.75), Inches(1.5),
Inches(11.5), Inches(5)
)
tf = content_box.text_frame
tf.word_wrap = True
# Paragraph 1: Bold heading
p1 = tf.paragraphs[0]
p1.text = "Executive Summary"
p1.font.size = Pt(24)
Related in office-docs
openpyxl
IncludedCreate and manipulate Microsoft Excel workbooks programmatically. Build spreadsheets with formulas, charts, conditional formatting, and pivot tables. Handle large datasets efficiently with streaming mode.
python-docx
IncludedCreate and manipulate Microsoft Word documents programmatically. Build reports, contracts, and documentation with full control over paragraphs, tables, headers, styles, and images.
docx-templates
IncludedTemplate-based Word document generation using Jinja2 syntax. Create reports, contracts, and documents with loops, conditionals, tables, and mail merge capabilities.
pypdf
IncludedManipulate PDF documents programmatically. Merge, split, rotate, and watermark PDFs. Extract text and metadata. Handle form filling and encryption/decryption.