Skip to main content
Practice

How to Automate Repetitive PowerPoint Tasks

Have you ever had to create the same format report weekly or monthly, or fill in data in a PPT template for certificates over and over again?

Manually modifying slides or repeating tasks can waste time and lead to errors.

python-pptx is a Python library that automates repetitive PPT tasks.

In this lesson, we will learn how to create a simple PPT slide using python-pptx.

Note: To run the practice code on your computer, you need to install the python-pptx library using the command pip install python-pptx.


Creating a PPT Slide with Python Code

Let's start by creating a simple presentation to learn the basics of using the python-pptx library.

To import the python-pptx library, use the format from pptx import {Class or Function} to import the necessary classes or functions.

In the code example below, we create a PowerPoint file object and add some simple text to a slide.

Creating a Simple Presentation
# Import the python-pptx library
from pptx import Presentation

# Create a new presentation object
prs = Presentation()

# Add the first slide
slide_content = prs.slides.add_slide(prs.slide_layouts[1])

# Select the title of the first slide
title_content = slide_content.shapes.title

# Select the body content (2nd placeholder) of the slide
content = slide_content.placeholders[1]

# Add text to the selected title
title_content.text = "Hello"

# Add text to the selected body content
content.text = "This is CodeFriends"

# Save the presentation file
prs.save("output_file.pptx")

placeholders refers to a property that references pre-defined spaces within the slide layout, known as placeholders.

Placeholders are content boxes located in specific positions on the slide, such as titles, subtitles, bodies, images, and tables.

placeholders[1] means the 2nd placeholder and can include various forms of content based on the PowerPoint slide layout.

If you run the above code, a PowerPoint file named output_file.pptx will be created.

Opening the file will reveal that a "Title" and "Body" have been added to the first slide.


Code Explanation

Creating a Simple Presentation
prs = Presentation()

python-pptx creates a new PowerPoint file object using the Presentation() method.


Adding a Slide
slide_content = prs.slides.add_slide(prs.slide_layouts[1])

You can add slides to the PowerPoint file object using slides.add_slide().

The parameter prs.slide_layouts[1] indicates the slide layout.

PowerPoint provides several standard layouts, and slide_layouts[1] is a layout that includes a title box and a content box for the body text.


Adding Title and Body Text
# Select the title of the first slide
title_content = slide_content.shapes.title

# Select the body content of the first slide
content = slide_content.placeholders[1]

# Add text to the selected title
title_content.text = "Hello"

# Add text to the selected body content
content.text = "This is CodeFriends"

We add the first slide to the slide_content variable and use slide_content.shapes.title to select the title box.

shapes represents all objects on the slide, and among the objects in the slide, title refers to the title box.

slide_content.placeholders[1] selects the content box where the body text can be inserted.

placeholders refer to a property that references pre-defined spaces within the slide layout, known as placeholders.

Placeholders are text boxes or content boxes located in specific positions on the slide that can contain titles, subtitles, body text, images, tables, and more.


Saving the PowerPoint File
prs.save("output_file.pptx")

Finally, the save method saves the PowerPoint file that was created with the presentation object.


Practice

Run the code and check the PowerPoint slide with the title and body content added.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.