
Creating a presentation slide deck isn’t restricted to presentation software itself. Designers often opt to create slide decks in tools such as Adobe Illustrator or CorelDRAW, while others prefer Adobe Photoshop or online tools like Canva. In this case, we’re going to bring a creative project for programming folks: how to make a presentation in Python.
This tutorial will cover, step by step, all the code required to create a slide deck using Python code.
Table of Contents
- Why Should You Create a PowerPoint Presentation in Python?
- Setting Up the Environment
- Importing Required Libraries
- Creating a New Presentation File in Python
- Defining Slide Dimensions in Python
- Adding Title Slide and Subtitle
- Adding Content to Slides
- Adding Additional Slides
- Adding an Image to a Slide in Python
- Adding a Text Box into a Slide in Python
- How to Style Text Boxes
- How to Create and Add Graphs to PowerPoint in Python
- How to Export a Presentation Made in Python
- FAQs
- Final Words
Why Should You Create a PowerPoint Presentation in Python?
Python allows you to automate the creation of presentations, which is essential when generating large volumes of reports or slide decks dynamically. For example, a weekly sales report or financial summary can be automatically generated from databases or APIs and outputted directly into a well-structured PowerPoint file, without human intervention. This scales far better than manually copying charts and text into PowerPoint.
We should also conside that Python can interface with any data source: SQL databases, Excel files, CSVs, APIs, or even real-time data streams. A script can pull fresh data, format it, generate charts (using libraries like matplotlib or plotly), and embed these into a PowerPoint presentation. This is extremely useful for BI teams, financial analysts, or any developer working with dashboards or KPIs.
Setting Up the Environment
To build a PowerPoint presentation in Python, you’ll need to install and configure a few libraries.
Install them using pip:
pip install python-pptx
Next, we need to install the Pillow library to be able to use images:
pip install Pillow
Importing Required Libraries
In your Python file, you need to import the installed libraries in order to use them.
from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor
from PIL import Image
from pptx.enum.shapes import MSO_AUTO_SHAPE_TYPE
from pptx.enum.dml import MSO_LINE_DASH_STYLE
import matplotlib.pyplot as plt
import pandas as pd
import os
Creating a New Presentation File in Python
We need to initialize a new PowerPoint file.
prs = Presentation()
Defining Slide Dimensions in Python
You can set custom dimensions for our slides depending on what we want to create. Remember to keep in mind the typical aspect ratios for slides.
prs.slide_width = Inches(16)
prs.slide_height = Inches(9)
Adding Title Slide and Subtitle
title_slide_layout = prs.slide_layouts[0] # Title slide layout
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "2025 Business Strategy"
subtitle.text = "Generated with Python Automation"
Adding Content to Slides
bullet_slide_layout = prs.slide_layouts[1] # Title and Content layout
slide = prs.slides.add_slide(bullet_slide_layout)
title = slide.shapes.title
content = slide.placeholders[1]
title.text = "Key Metrics"
text_frame = content.text_frame
text_frame.text = "• Revenue up 15%\n• Market share stable\n• Churn down 3%"
You can use .add_paragraph() and .level for bullet hierarchy and formatting:
p = text_frame.add_paragraph()
p.text = "Customer satisfaction at 87%"
p.level = 1
We can also add hyperlinks to a slide in Python:
run = text_frame.paragraphs[0].add_run()
run.text = "Visit site"
run.hyperlink.address = "https://example.com"
Customizing Fonts in Slides
run = text_frame.paragraphs[0].runs[0]
font = run.font
font.name = 'Arial'
font.size = Pt(20)
font.bold = True
font.color.rgb = RGBColor(0, 0, 255) # Blue
Adding Additional Slides
We can easily add more slides to this Python presentation deck with a bit of code.
slide_layout = prs.slide_layouts[2]
slide = prs.slides.add_slide(slide_layout)
title = slide.shapes.title
body = slide.placeholders[2]
title.text = “Reviewing Q1 Data”
body.text = “placeholder text”
Adding an Image to a Slide in Python
Pillow allows you to check image formats, resize or compress images, and validate dimensions before inserting them. This reduces slide bloat and layout issues.
Preparing the Image with Pillow
from PIL import Image
def resize_image(image_path, max_width=800, max_height=600):
img = Image.open(image_path)
img.thumbnail((max_width, max_height))
new_path = "resized_" + os.path.basename(image_path)
img.save(new_path)
return new_path
Inserting the Image to the Slide
img_slide_layout = prs.slide_layouts[6] # Blank layout
slide = prs.slides.add_slide(img_slide_layout)
image_path = resize_image("sales_graph.png")
left = Inches(1)
top = Inches(2)
height = Inches(4)
slide.shapes.add_picture(image_path, left, top, height=height)
Add Multiple Images
slide.shapes.add_picture(resize_image("logo.png"), Inches(0.5), Inches(0.5), height=Inches(1))
slide.shapes.add_picture(resize_image("chart.png"), Inches(3), Inches(1.5), height=Inches(3))
Adding a Text Box into a Slide in Python
To add a custom-positioned text box, you use slide.shapes.add_textbox(), specifying its position and dimensions.
slide = prs.slides.add_slide(prs.slide_layouts[5]) # Blank slide
# Define position and size (left, top, width, height)
left = Inches(1)
top = Inches(2)
width = Inches(5)
height = Inches(1.5)
textbox = slide.shapes.add_textbox(left, top, width, height)
text_frame = textbox.text_frame
text_frame.text = "This is a custom text box."
You can also append paragraphs:
p = text_frame.add_paragraph()
p.text = "Second paragraph in the same box."
How to Style Text Boxes
We can make several changes to our text boxes in Python, except for rounded corners or shadow effects, as they are not supported by python-pptx.
Changing the Background Color of a Text Box
# Assume you have already added the text box
textbox = slide.shapes.add_textbox(Inches(1), Inches(1), Inches(4), Inches(1.5))
text_frame = textbox.text_frame
text_frame.text = "Styled Text Box"
# Change background color
fill = textbox.fill
fill.solid()
fill.fore_color.rgb = RGBColor(200, 230, 255) # Light blue
Setting Borders (Outline Color and Width)
# Set border (line) color and width
line = textbox.line
line.color.rgb = RGBColor(0, 0, 0) # Black border
line.width = Pt(2)
As an optional feature, we can turn the borders into dashed style.
line.dash_style = MSO_LINE_DASH_STYLE.DASH
You can also remove the border:
line.fill.background()
How to Create and Add Graphs to PowerPoint in Python
Generate the Graph with matplotlib
# Example data (can be loaded from any file)
data = pd.read_csv("sales_data.csv")
months = data["Month"]
sales = data["Revenue"]
# Create the plot
plt.figure(figsize=(10, 6))
plt.plot(months, sales, marker='o', linestyle='-', color='green')
plt.title("Monthly Sales Performance")
plt.xlabel("Month")
plt.ylabel("Revenue")
plt.grid(True)
# Save the figure
chart_path = "sales_chart.png"
plt.savefig(chart_path, bbox_inches='tight')
plt.close()
Add the Graph Image to Your Python Presentation
# Optional: Resize image with Pillow
img = Image.open(chart_path)
img = img.resize((1000, 600))
img.save(chart_path)
# Load presentation and insert image
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = Inches(1)
top = Inches(1)
height = Inches(4.5)
slide.shapes.add_picture(chart_path, left, top, height=height)
Keep in mind that graphs added from Python as image files (PNG, JPG, etc.) CANNOT be edited inside PowerPoint. You won’t be able to:
- Modify axis labels, data points, or chart styles within PowerPoint
- Change chart types (e.g., from line to bar)
- Use PowerPoint’s chart animation features
To make the chart editable in PowerPoint:
- You’d need to recreate it manually inside PowerPoint using Office charts, or
- Use VBA scripting or COM automation (only possible on Windows, not cross-platform)
Can You Import Graph Data from Python Files?
Yes. You can generate graphs from:
- .csv – pd.read_csv(“filename.csv”)
- .xlsx – pd.read_excel(“filename.xlsx”)
- .json – pd.read_json(“filename.json”)
- SQL database – pd.read_sql(query, connection)
Once the data is loaded into a DataFrame, use matplotlib or seaborn to create your graph.
How to Export a Presentation Made in Python
Exporting is straightforward: use the .save() method at the end of your script.
prs.save("my_presentation.pptx")
It’s a good practice to avoid unintentional overwriting. This can be done by enabling a filename check.
filename = "my_presentation.pptx"
if os.path.exists(filename):
os.remove(filename)
prs.save(filename)
If, by chance, you want to export your PowerPoint presentation made in Python to another folder, use this code:
prs.save("/path/to/your/folder/custom_presentation.pptx")
FAQs
What is python-pptx used for?
It’s a Python library for creating and editing PowerPoint (.pptx) files, including slides, text, images, and tables.
Why do I need Pillow if I already have python-pptx?
Pillow enables you to preprocess images (e.g., resize, format-check, convert modes), which is essential before inserting them into a PowerPoint to avoid compatibility or sizing issues.
Can I use a PowerPoint template (.pptx) as a base?
Yes. You can load an existing template:
prs = Presentation(“company_template.pptx”)
Can I edit an existing PowerPoint instead of creating a new one?
Yes. Load it like this:
prs = Presentation(“existing.pptx”)
You can then add, remove, or modify slides.
Is it possible to export the presentation as PDF?
Not directly with python-pptx. Use Microsoft PowerPoint (CLI or GUI), LibreOffice, or comtypes on Windows to convert .pptx to .pdf.
Final Words
Creating a PowerPoint presentation in Python is a bit like flexing your creative coding muscles to build something outside the box. It may seem taxing, but it gives you an actual understanding of how much knowledge you gathered on the programming language itself.
Also, for programmers that don’t feel comfortable working with Microsoft Office or lack a subscription, this technique can be an alternative, as they can customize these slides by uploading the presentation file directly into Google Slides.