Create Presentations with Python-pptx

Here is the code to make a presentation in powerpoint with Python and the module python-pptx.

We created a class that takes as a argument a list with

  • title of the slide
  • subtitle
  • number of layout (0 is for the first slide, 1 is for the title + bullet points … ecc.)

Creating a list o list with the three arguments, you can easily create a whole presentation (for now with just text).

from pptx import Presentation
import os

# Create a powerpoint file
prs = Presentation()

class Slide:

	def __init__(self, data):
		# data is a list with title, subtitle and layout num
		print(data)
		self.layout = prs.slide_layouts[data[2]]
		self.slide = prs.slides.add_slide(self.layout)
		self.title = self.slide.shapes.title
		self.title.text = data[0]
		self.subtitle = self.slide.placeholders[1]
		self.subtitle.text = data[1]

def save(name):
	prs.save(name)
	# Open the file on the operating system
	os.startfile(name)

# title / subtitle / layout num
slides = [
["Hello people", "We are great", 1]
]

for s in slides:
	Slide(s)

save("prova.pptx")

Python and Powerpoint


Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.