Lecture

Automating Repetitive PowerPoint Tasks

Have you ever created a report in the same format every week or month or filled a PowerPoint template like a certificate with different data each time?

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

python-pptx is a Python library designed to automate repetitive PPT tasks.

In this lesson, we will learn how to create simple PowerPoint slides using python-pptx.

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


How to Create PowerPoint Slides Using Python Code

Let’s start with a simple presentation to understand the basic usage of the python-pptx library.

When importing the python-pptx library, use the format from pptx import {class or function} to bring in the required components.

The following example creates a PowerPoint file object and adds 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) 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 is an attribute that refers to predefined spaces in a slide layout, known as placeholders.

Placeholders are content boxes that exist at specific positions in a slide, such as for titles, subtitles, body content, images, and tables.

placeholders[1] refers to the second placeholder, which may contain different types of content depending on the PowerPoint slide layout.

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

Opening the file will show the first slide with a "title" and "body content" added.


Code Explanation

Creating a Simple Presentation
prs = Presentation()

Presentation() is a method in python-pptx used to create a new PowerPoint file object.


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

slides.add_slide() is used to add a slide to the PowerPoint file object.

The argument prs.slide_layouts[1] represents the slide layout.

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


Adding a Title and Body Content
# 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"

The slide_content variable contains the added first slide, and slide_content.shapes.title selects the title box.

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

slide_content.placeholders[1] selects the content box for the body text.

placeholders is an attribute that refers to predefined spaces in the slide layout, such as placeholders for titles, subtitles, body content, images, and tables.


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

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


Practice

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

Mission
0 / 1

What is the most appropriate code to fill in the blank below?

python-pptx uses to select predefined layout content in a slide.
title
placeholders
save()
add_slide()

Lecture

AI Tutor

Publish

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result

Result

The document is empty.

Try running the code.