Search This Blog

Automating PowerPoint Presentations with Python

 

Automating PowerPoint Presentations with Python

PowerPoint presentations are commonly used for business reports, academic lectures, and data visualization. Python provides the python-pptx library to automate tasks like creating, editing, and formatting PowerPoint slides.


Installing Required Library

To work with PowerPoint files in Python, install the python-pptx library:

pip install python-pptx
  • python-pptx: Creates, edits, and formats PowerPoint presentations (.pptx).

Creating a New PowerPoint Presentation

Generating a Basic Presentation

from pptx import Presentation

# Create a new presentation
presentation = Presentation()

# Save the presentation
presentation.save("automated_presentation.pptx")
print("PowerPoint file created successfully.")

Adding Slides to a Presentation

from pptx import Presentation

# Create a new PowerPoint file
presentation = Presentation()

# Define slide layout (0 = Title Slide, 1 = Title and Content, etc.)
slide_layout = presentation.slide_layouts[1]

# Add a new slide
slide = presentation.slides.add_slide(slide_layout)

# Add title and content
slide.shapes.title.text = "Python PowerPoint Automation"
slide.placeholders[1].text = "Automate presentations using Python."

# Save the file
presentation.save("automated_presentation.pptx")

Adding Text, Images, and Tables

Adding Bulleted Lists

slide = presentation.slides.add_slide(presentation.slide_layouts[1])
title = slide.shapes.title
content = slide.placeholders[1]

title.text = "Benefits of PowerPoint Automation"
content.text = "• Saves time\n• Reduces manual errors\n• Improves productivity"

presentation.save("automated_presentation.pptx")

Adding an Image to a Slide

from pptx.util import Inches

slide = presentation.slides.add_slide(presentation.slide_layouts[5])

# Define image path
img_path = "image.png"

# Insert image
left = Inches(2)
top = Inches(2)
slide.shapes.add_picture(img_path, left, top, width=Inches(4), height=Inches(3))

presentation.save("automated_presentation.pptx")

Adding a Table to a Slide

slide = presentation.slides.add_slide(presentation.slide_layouts[5])

# Define table position and size
rows, cols = 3, 3
left, top, width, height = Inches(1), Inches(1), Inches(5), Inches(2)
table = slide.shapes.add_table(rows, cols, left, top, width, height).table

# Fill table with data
table.cell(0, 0).text = "Product"
table.cell(0, 1).text = "Price"
table.cell(0, 2).text = "Quantity"
table.cell(1, 0).text = "Laptop"
table.cell(1, 1).text = "$1200"
table.cell(1, 2).text = "5"
table.cell(2, 0).text = "Phone"
table.cell(2, 1).text = "$800"
table.cell(2, 2).text = "10"

presentation.save("automated_presentation.pptx")

Modifying and Formatting Slides

Changing Slide Background Color

from pptx.dml.color import RGBColor

slide = presentation.slides.add_slide(presentation.slide_layouts[5])
background = slide.background
fill = background.fill
fill.solid()
fill.fore_color.rgb = RGBColor(255, 0, 0)  # Red background

presentation.save("automated_presentation.pptx")

Formatting Text (Font Size, Bold, Color)

from pptx.util import Pt
from pptx.dml.color import RGBColor

slide = presentation.slides.add_slide(presentation.slide_layouts[5])
text_box = slide.shapes.add_textbox(Inches(1), Inches(1), Inches(5), Inches(1))
text_frame = text_box.text_frame

paragraph = text_frame.add_paragraph()
paragraph.text = "Formatted Text Example"

# Apply font styles
paragraph.font.size = Pt(24)
paragraph.font.bold = True
paragraph.font.color.rgb = RGBColor(0, 0, 255)  # Blue color

presentation.save("automated_presentation.pptx")

Extracting Content from PowerPoint Files

Reading Text from Slides

from pptx import Presentation

presentation = Presentation("automated_presentation.pptx")

for slide in presentation.slides:
    for shape in slide.shapes:
        if hasattr(shape, "text"):
            print(shape.text)

Extracting Images from a PowerPoint File

import os

presentation = Presentation("automated_presentation.pptx")

for i, slide in enumerate(presentation.slides):
    for j, shape in enumerate(slide.shapes):
        if shape.shape_type == 13:  # Picture type
            image = shape.image
            with open(f"extracted_image_{i}_{j}.png", "wb") as img_file:
                img_file.write(image.blob)

Automating PowerPoint Reports

Generating a Presentation from Excel Data

import pandas as pd
from pptx import Presentation

# Load data from Excel
df = pd.read_excel("sales_data.xlsx")

# Create presentation
presentation = Presentation()
slide_layout = presentation.slide_layouts[1]

# Add a slide for each row in Excel
for _, row in df.iterrows():
    slide = presentation.slides.add_slide(slide_layout)
    slide.shapes.title.text = f"Product: {row['Product']}"
    slide.placeholders[1].text = f"Sales: ${row['Sales']}"

presentation.save("sales_presentation.pptx")

Merging Multiple PowerPoint Presentations

from pptx import Presentation

ppt_files = ["file1.pptx", "file2.pptx"]
merged_presentation = Presentation()

for ppt_file in ppt_files:
    presentation = Presentation(ppt_file)
    
    for slide in presentation.slides:
        merged_presentation.slides.add_slide(slide)

merged_presentation.save("merged_presentation.pptx")

Conclusion

This section covered automating PowerPoint tasks, including creating, modifying, formatting slides, adding text, images, and tables, and extracting content. These techniques are useful for generating reports, business presentations, and automated slide creation from data sources.

Would you like additional examples or modifications?

Popular Posts