Python: How to create a PowerPoint document

Introduction

In this tutorial, we will learn how to create a PowerPoint document programmatically from a Python script. To do so, we will use the python-pptx module, which can be installed using pip with the following command:

pip install python-pptx

This module has a very extensive API that allows not only to create new documents and adding content, but also to access existing documents and edit them. You can check the API documentation here and the GitHub project page here.

These features can be useful to programmatically automate some PowerPoint related tasks (ex: producing a report every week from data taken from a database).

This tutorial was tested on Windows 8.1, using Python version 3.7.2 and version 2010 of Microsoft PowerPoint.

Creating a PowerPoint document

The first thing we will do is importing the Presentation function from the pptx module.

from pptx import Presentation

After this, we will call the Presentation function we just imported, without passing any arguments. As output, this function will return an object of class Presentation.

presentation = Presentation()

Now that we have our Presentation object, we will add a slide to it. In order to add a slide, we will need to use a layout from a slide master. Note that even if you never interacted directly with this feature from PowerPoint, when you create a new document there is a default slide master and some slide layouts. Naturally, in a PowerPoint file, we can then edit the slide master if we want.

To create our slide, we will use the default slide master and its first layout. To get the slide masters available in our Presentation object, we access the slide_masters property, which is an object of class SlideMasters. This object corresponds to a sequence of SlideMaster objects and thus we can access it with list semantics.

We will access the first element of this list. Since we have just created our presentation, it will only have the default slide master but, if we were working with an existing presentation, there could be multiple slide masters.

On our SlideMaster, we will access the slide_layouts property, which is an object of class SlideLayouts. Once again, this class corresponds to a sequence of SlideLayout objects and supports list semantics. We will also access the first layout of the list.

layout = presentation.slide_masters[0].slide_layouts[0]

Now that we have our layout, we can finally add the new slide to the presentation. To do so, we access the slides property of our Presentation object and call the add_slide method.

As input, we pass the layout we want to use, which we have already stored in a variable.

slide = presentation.slides.add_slide(layout)

To finish, we will save our presentation to get a PowerPoint file in our file system. To do so, we just need to call the save method on the Presentation object, passing as input the path to where we want to save the file (which should include the file name with the extension).

When testing the code, don’t forget to use a path in your file system.

presentation.save('C:/Users/N/Desktop/presentation.pptx')

The complete code for this section can be seen below.

from pptx import Presentation

presentation = Presentation()
layout = presentation.slide_masters[0].slide_layouts[0]
presentation.slides.add_slide(layout)

presentation.save('C:/Users/N/Desktop/presentation.pptx')

To test the previous code, simply run it in a tool of your choice. In my case, I’m using PyCharm, a Python IDE. After running the code, you should get a new PowerPoint file in the location of the file system you passed to the save method.

Note that, if you open the document, it should contain a slide.

Adding a textbox to the slide

In this section we will build upon the previous code to add a textbox to our slide. We will then write some content to that textbox and set its background color to blue.

Like before, we will import the Presentation function from the pptx module. We will also import the Cm class from the pptx.util sub-module and the RGBColor class from the pptx.dml.color sub-module. We will check the usage of these two classes below.

from pptx import Presentation
from pptx.util import Cm
from pptx.dml.color import RGBColor

After this, we will do what we did in the previous section: creating a Presentation object and adding a slide to it.

Note that the add_slide method we used before returns an object of class Slide. This time, we will store it in a variable.

presentation = Presentation()
layout = presentation.slide_masters[0].slide_layouts[0]
slide = presentation.slides.add_slide(layout)

Now, to add a textbox to our slide, we access the shapes property, which is an object of class SlideShapes. Then, we call the method add_textbox. This method receives the following four inputs:

  • X position of the top left corner of the textbox;
  • Y position of the top left corner of the textbox;
  • Width of the textbox;
  • Height of the textbox.

To help us setting the coordinates and dimensions of the textbox, we will use the Cm class to specify them in centimeters. You can check the other helper classes available here, which allow to use other units.

Note that this method call will return a Shape object, which we will store in a variable, so we can configure other properties.

text_box = slide.shapes.add_textbox(Cm(1), Cm(1), Cm(5), Cm(2))

Now that we have our textbox, we will set a text to it. To do so, we simply need to access the text property and assign a value.

text_box.text = "My slide"

To set a background color for the textbox, we will access the fill property of our shape. This is an object of class FillFormat. We will first call the solid method, to set the fill type to solid.

Then, we will access the fore_color property of the FillFormat object, which is an object of class ColorFormat. Finally, we will access the rgb property of this last object and assign to it an object of class RGBColor representing the color we want (in our case, we will set it to blue).

text_box.fill.solid()
text_box.fill.fore_color.rgb = RGBColor(0, 0, 255)

The complete code can be seen below and already includes saving the file at the end.

from pptx import Presentation
from pptx.util import Cm
from pptx.dml.color import RGBColor

presentation = Presentation()
layout = presentation.slide_masters[0].slide_layouts[0]
slide = presentation.slides.add_slide(layout)

text_box = slide.shapes.add_textbox(Cm(1), Cm(1), Cm(5), Cm(2))
text_box.text = "My slide"

text_box.fill.solid()
text_box.fill.fore_color.rgb = RGBColor(0, 0, 255)


presentation.save('C:/Users/N/Desktop/presentation.pptx')

Upon running the code, you should again get a PowerPoint document in your file system (if you use the same name as the file from the previous section, it will overwrite the existing one).

This time, if you open it, you should see a blue textbox in the slide, like shown in figure 1. Note that the adicional textboxes seen there (title and subtitle) are added by default because of the default slide layout I have used.

PowerPoint slide generated from the Python script.
Figure 1 – PowerPoint slide generated from the Python script.

Leave a Reply

%d bloggers like this: