How to create powerpoint files using Python


We’ve all had to make PowerPoint presentations at some point in our lives. Most often we’ve used Microsoft’s PowerPoint or Google Slides.

But what if you don’t have membership or access to the internet? Or what if you just wanted to do it the “programmers” way?

Well, worry not for Python’s got your back!

In this article you’ll learn how to create a PowerPoint file and add some content to it with the help of Python. So let’s get started!

Getting Started

Throughout this walkthrough, we’ll be using the python-pptx package. This package supports different python versions ranging from 2.6 to 3.6.

So, make sure you have the right version of Python installed on your computer first.

Next, open your terminal and type −

pip install python-pptx

Once the module is successfully installed, you are all set to start coding!

Importing the Modules

Before we get into the main aspects of it, we must first import the right modules to utilise the various features of the package.

So, let’s import the presentation class that contains all the required methods to create a PowerPoint.

from pptx import Presentation

Now, we are all set to create a presentation.

Creating a Presentation

Let us now create an object of the Presentation class to access its various methods.

X = Presentation()

Next, we need to select a layout for the presentation.

As you can see, there are nine different layouts. In the pptx module, each layout is numbered from 0 to 8. So, “Title Slide” is 0 and the “Picture with Caption” is 8.

So, let us first add a title slide.

Layout = X.slide_layouts[0] first_slide = X.slides.add_slide(Layout) # Adding first slide

Now, we have created a layout and added a slide to our presentation.

Let us now add some content to the first slide.

first_slide.shapes.title.text = "Creating a powerpoint using Python"

first_slide.placeholders[1].text = "Created by Tutorialpoints"

In the above lines, we first add a title to the “first slide” and a subtitle using the placeholder.

Now, let us save the presentation. We can do this using the save command."First_presentation.pptx")

If you run the program, it will save the PowerPoint presentation in the directory where your program is saved.


You have successfully created your PowerPoint presentation.

Creating a second slide and adding some content

Firstly, you’ll need to import additional methods to add content.

from pptx import Presentation

from pptx.util import Inches

Let us first create and add the second slide.

Second_Layout = X.slide_layouts[5]

second_slide = X.slides.add_slide(Second_Layout)

Adding title for the next slide,

second_slide.shapes.title.text = "Second slide"

Now, we must create a textbox and move its layout to suit our needs.

Let us position it and adjust its margins in inches.

textbox = second_slide.shapes.add_textbox(Inches(3), Inches(1.5),Inches(3), Inches(1))

The above line of code will place a textbox 3 Inches from left and 1.5 Inches from the top with a width of 3 Inches and height of 1 Inch.

Once we have the layout and position fixed, time to create a textframe to add content to.

textframe = textbox.text_frame

Now to add a paragraph of content,

paragraph = textframe.add_paragraph()
paragraph.text = "This is a paragraph in the second slide!"

Finally, save the presentation again using the save method."First_presentation.pptx")



# Creating powerpoint presentations using the python-pptx package

from pptx import Presentation
from pptx.util import Inches

X = Presentation()

Layout = X.slide_layouts[0]
first_slide = X.slides.add_slide(Layout)

first_slide.shapes.title.text = "Creating a powerpoint using Python" first_slide.placeholders[1].text = "Created by Tutorialpoints""First_presentation.pptx")

Second_Layout = X.slide_layouts[5]
second_slide = X.slides.add_slide(Second_Layout)
second_slide.shapes.title.text = "Second slide"

textbox = second_slide.shapes.add_textbox(Inches(3), Inches(1.5),Inches(3), Inches(1)) textframe = textbox.text_frame
paragraph = textframe.add_paragraph()
paragraph.text = "This is a paragraph in the second slide!""First_presentation.pptx")


That’s it! You can now create your own presentation with the help of Python.

And there are a lot more features within the pptx package that allows you to completely customise your presentation from A-Z just the way you do it in GUI.

You can add images, build charts, display statistics and a lot more.

You can go through python-pptx official documentation for more syntaxes and features.