How to create powerpoint files using Python

Creating PowerPoint presentations programmatically can be incredibly useful for automating report generation, batch processing, or when you don't have access to Microsoft Office. Python's python-pptx library provides a powerful way to create and manipulate PowerPoint files programmatically.

Installation

First, install the python-pptx package using pip ?

pip install python-pptx

Creating a Basic Presentation

Let's start by creating a simple presentation with a title slide ?

from pptx import Presentation

# Create a presentation object
prs = Presentation()

# Select title slide layout (layout 0)
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)

# Add title and subtitle
title = slide.shapes.title
subtitle = slide.placeholders[1]

title.text = "Creating PowerPoint with Python"
subtitle.text = "Automated Presentation Generation"

# Save the presentation
prs.save('basic_presentation.pptx')
print("Presentation created successfully!")
Presentation created successfully!

Understanding Slide Layouts

PowerPoint has 9 built-in slide layouts, numbered from 0 to 8 ?

PowerPoint Slide Layouts 0: Title Slide (Title + Subtitle) 1: Title and Content 2: Section Header 3: Two Content (Side by side) 4: Comparison (Two columns) 5: Title Only (Blank content) 6: Blank (No placeholders) 7: Content with Caption 8: Picture with Caption Use slide_layouts[index] to select a specific layout

Adding Content Slides

Let's create a more comprehensive presentation with multiple slides and content ?

from pptx import Presentation
from pptx.util import Inches

# Create presentation
prs = Presentation()

# Title slide
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Python Automation Report"
subtitle.text = "Generated automatically with python-pptx"

# Content slide with bullet points
bullet_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(bullet_slide_layout)
shapes = slide.shapes

title_shape = shapes.title
body_shape = shapes.placeholders[1]

title_shape.text = 'Key Benefits of Python Automation'

# Add bullet points
tf = body_shape.text_frame
tf.text = 'Saves time and reduces manual errors'

p = tf.add_paragraph()
p.text = 'Consistent formatting across presentations'
p.level = 0

p = tf.add_paragraph()
p.text = 'Easy to integrate with data sources'
p.level = 0

# Blank slide with custom textbox
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

# Add custom positioned textbox
left = Inches(2)
top = Inches(1)
width = Inches(4)
height = Inches(2)

textbox = slide.shapes.add_textbox(left, top, width, height)
text_frame = textbox.text_frame
text_frame.text = "This is a custom textbox positioned at 2 inches from left and 1 inch from top."

# Save presentation
prs.save('comprehensive_presentation.pptx')
print("Comprehensive presentation created!")
Comprehensive presentation created!

Working with Text Formatting

You can also format text with different styles and properties ?

from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor

prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[6])

# Create textbox with formatted text
textbox = slide.shapes.add_textbox(Inches(1), Inches(1), Inches(8), Inches(6))
text_frame = textbox.text_frame

# Add formatted paragraph
p = text_frame.paragraphs[0]
p.text = "Formatted Text Example"
p.font.name = 'Arial'
p.font.size = Pt(24)
p.font.bold = True
p.font.color.rgb = RGBColor(255, 0, 0)  # Red color

# Add another paragraph
p = text_frame.add_paragraph()
p.text = "This text is blue and italic"
p.font.size = Pt(18)
p.font.italic = True
p.font.color.rgb = RGBColor(0, 0, 255)  # Blue color

prs.save('formatted_presentation.pptx')

Complete Example

Here's a complete example that demonstrates creating a full presentation ?

from pptx import Presentation
from pptx.util import Inches

def create_presentation():
    # Initialize presentation
    prs = Presentation()
    
    # Title slide
    title_slide = prs.slides.add_slide(prs.slide_layouts[0])
    title = title_slide.shapes.title
    subtitle = title_slide.placeholders[1]
    title.text = "Monthly Sales Report"
    subtitle.text = "Automated Report - Python Generated"
    
    # Content slide
    content_slide = prs.slides.add_slide(prs.slide_layouts[1])
    title = content_slide.shapes.title
    content = content_slide.placeholders[1]
    
    title.text = "Sales Highlights"
    tf = content.text_frame
    tf.text = "Q1 sales increased by 15%"
    
    # Add more bullet points
    for point in ["New customers: 250", "Revenue: $2.5M", "Growth rate: 12%"]:
        p = tf.add_paragraph()
        p.text = point
        p.level = 0
    
    # Summary slide
    summary_slide = prs.slides.add_slide(prs.slide_layouts[5])
    title = summary_slide.shapes.title
    title.text = "Summary"
    
    # Add custom textbox
    textbox = summary_slide.shapes.add_textbox(
        Inches(1), Inches(2), Inches(8), Inches(4)
    )
    textbox.text_frame.text = "This presentation was generated automatically using Python and python-pptx library. All data and formatting were applied programmatically."
    
    # Save presentation
    prs.save('sales_report.pptx')
    return "Presentation created successfully!"

# Create the presentation
result = create_presentation()
print(result)
Presentation created successfully!

Key Features

Feature Method/Property Description
Create Presentation Presentation() Initialize new presentation
Add Slide slides.add_slide(layout) Add slide with specific layout
Set Title shapes.title.text Set slide title
Add Textbox shapes.add_textbox() Add custom positioned text
Save File save(filename) Save presentation to file

Conclusion

The python-pptx library provides a powerful way to create PowerPoint presentations programmatically. You can automate report generation, create consistent formatting, and integrate with data sources to build dynamic presentations. This approach is particularly useful for batch processing and automated reporting workflows.

Updated on: 2026-03-25T16:00:16+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements