How to create slides for powerpoint with Python

This is an improvement of some code I made to use powerpoint with python

from pptx import Presentation
import os

pres = Presentation()

# Slides layout types
def style(x):
    ''' returns a slide_layout '''
    return pres.slide_layouts[x]


def add_slide(layout, title, content):
    ''' add a slide with layout style = x)  '''
    slide0 = pres.slides.add_slide(style(layout))
    title0 = slide0.shapes.title
    title0.text = title
    content0 = slide0.placeholders[1]
    content0.text = content[0]
    if layout == 3:
        content1 = slide0.placeholders[2]
        content1.text = content[1]


def presentation():
    ''' here you put your slides '''
    add_slide(
        layout=0,
        title="My title",
        content=["My subtitle"])

    add_slide(
        layout=1,
        title="First page",
        content=["Subtitle first page"])

    # the layoyt 3 has 2 placeholders
    add_slide(
        layout=3,
        title="Second page",
        content=["left", "right"])


def start_presentation():
    presentation()
    title = "text.pptx"
    try:
        pres.save(title)
    except PermissionError:
        os.system("TASKKILL /F /IM powerpnt.exe")
        pres.save(title)
        print("The previously opened powerpoint file was closed")
    os.system("start " + title)


start_presentation()

look

powerpoint with python

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

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.