How to Create a PowerPoint Presentation Using Python?

Estimated read time 2 min read

You can create a PowerPoint presentation using Python and the python-pptx library. Here’s an example code snippet:

from pptx import Presentation
from pptx.util import Inches

# create presentation object
prs = Presentation()

# add a slide with a title and subtitle
slide = prs.slides.add_slide(prs.slide_layouts[1])
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "My Python-generated Presentation"
subtitle.text = "Created with python-pptx"

# add an image to the slide
img_path = 'path/to/image.png'
pic = slide.shapes.add_picture(img_path, Inches(1), Inches(2))

# save the presentation'my_presentation.pptx')

In this example, we first import the Presentation and Inches classes from the pptx.util module. We then create a new Presentation object using prs = Presentation().

We add a new slide to the presentation using slide = prs.slides.add_slide(prs.slide_layouts[1]). We use the slide_layouts attribute of the presentation object to choose a slide layout. In this case, we’re using the layout with a title and subtitle.

We add a title and subtitle to the slide using the title and placeholders attributes of the slide object. We set the text of these placeholders using the text attribute.

We add an image to the slide using the add_picture() method of the slide.shapes object. We specify the path to the image file and the position and size of the image on the slide using the Inches() function.

Finally, we save the presentation to a file using the save() method of the Presentation object.

You can customize the presentation further by adding more slides, text boxes, images, charts, and other elements using the methods and properties provided by the python-pptx library.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply