Python Program to Generate Powerpoint Presentation written by Nischal Lal Shrestha.
Image of Nischal who is author of the post.

By Nischal Lal Shrestha

On Tue 24 July 2018

Python Program to Generate Powerpoint Presentation

Meet Bill!!

He is an employee at GreenPlus Softwares. His boss at GreenPlus loves the presentation, So he is asked to prepare presentation every day.

One day Bill thought, "Why can't I run a program and generate a presentation instead of doing it manually all over again?"

You and Bill are close friends, So Let's help Bill to create such program.

Installing python-pptx

python-pptx is a Python library for creating and updating PowerPoint (.pptx) files. So Let's install python-pptx.

python-pptx is hosted on PyPI, so installing with pip is simple. Python and pip must be installed earlier.

pip install python-pptx

Creating New Presentation

Creating new presentation is very simple. Just import the Presentation class from pptx and save it using any name you desire.

from pptx import Presentation

prs = Presentation() # Simple Presentation Object'yourSuitableName.pptx')

This creates a new presentation from the built-in default template and saves it unchanged to a file named ‘yourSuitableName.pptx. Let's add some Slide to it.

PowerPoint Slide 101

The slide layouts in a standard PowerPoint theme always occur in the same sequence. This allows content from one deck to be pasted into another and be connected with the right new slide layout:

  • Title (presentation title slide)
  • Title and Content
  • Section Header (sometimes called Segue)
  • Two Content (side by side bullet textboxes)
  • Comparison (same but additional title for each side by side content box)
  • Title Only
  • Blank
  • Content with Caption
  • Picture with Caption

In python-pptx, these are prs.slide_layouts[0] through prs.slide_layouts[8]. i.e If you want to add a slide with Title and Content you have to use prs.slide_layouts[0], here prs is presentation Object which can be named anything you desire. Keep in mind that, there’s no rule they have to appear in this order, it’s just a convention followed by the themes provided with PowerPoint.

Adding a Slide

from pptx import Presentation

prs = Presentation() # Simple Presentation Object

SLIDE_LAYOUT_TITLE_AND_CONTENT = 1 # 1 is a slide with Presentation Title
slide1_layout = prs.slide_layouts[SLIDE_LAYOUT_TITLE_AND_CONTENT] # Select a slide with Presentation Title
slide1 = prs.slides.add_slide(slide1_layout) # Appends the slide1_layout at the end of collections
slide1.shapes.title.text = "Watch Out!!" # Adds Some Shapes
slide1.placeholders[1].text = "The world is Spinning."'watchout.pptx')

Name this .py file anything you desire like If you run this file you will see that a new .pptx file has been created in your working directory.

Note that I am not using MS powerpoint but it works correctly here too.

Presentation File in a List

Now open the file you will see this

Presentation with 1 Selected

If you set SLIDE_LAYOUT_TITLE_AND_CONTENT = 3, then you would see this

Presentation with 3 Selected