Skip to content

Automatically Creating Powerpoints using ChatGPT and Python

Introduction

In this post we look at how you can combine ChatGPT and Python to create Powerpoint files automatically.

Getting Started

Before we can start coding we need to do a few things first:

1 Install the Python PPTX module

Install python-pptx (and dependencies)

2. Install the OpenAI Python Module

Install OpenAI Python module

3. Sign up for the API and generate an API Key

Sign up for the OpenAI API here

Tutorial Video

Project Source Code

Here is the full source code for reference as you follow along with the video.

import openai, json
from pptx import Presentation
openai.api_key = "YOUR_API_KEY_HERE"

presentation_title = input("What do you want to make a presentation about?")

query_json = """"{
    "input_text": "[[QUERY]]",
    "output_format": "json",
    "json_structure": {
        "slides":"{{presentation_slides}}"
       }
    }"""
       
question = "Generate a 10 slide presentation for the topic. Produce 50 to 60 words per slide. " + presentation_title + ".Each slide should have a  {{header}}, {{content}}. The final slide should be a list of discussion questions. Return as JSON."

prompt = query_json.replace("[[QUERY]]",question)
print(prompt)
completion = openai.ChatCompletion.create(model = "gpt-3.5-turbo", messages =[{"role":"user","content":prompt}])
response = completion.choices[0].message.content

print(response)

r = json.loads(response)

slide_data = r["slides"]

prs = Presentation()

for slide in slide_data:
    slide_layout = prs.slide_layouts[1]
    new_slide = prs.slides.add_slide(slide_layout)
    
    if slide['header']:
        title = new_slide.shapes.title
        title.text = slide['header']
    
    if slide['content']:
       shapes = new_slide.shapes
       body_shape = shapes.placeholders[1]
       tf = body_shape.text_frame
       tf.text = slide['content']
       tf.fit_text(font_family="Calibri", max_size=18, bold=True)
       
prs.save("output.pptx")