If you are looking for automating the generation and manipulation of PowerPoint presentations in Python, then this article is going to help you a lot. Because in this article, you are going to learn how to create a PowerPoint PPT or PPTX from scratch in Python. Also, you will come to know how to add various elements in the PPT slide programmatically.

Create and modify PowerPoint ppt pptx in Python

MS PowerPoint is a popular and commonly used application that lets you create interactive presentations using charts, graphics, animations, and other elements. Let’s see how to implement various useful PowerPoint features in a Python application. So after reading this article, you will be capable of creating PowerPoint PPT or PPTX in Python from scratch. In addition, we will demonstrate how to add slides, text, tables, images, and charts in the PPT presentations dynamically.

Python Library to Create PowerPoint PPT - Free Download

Aspose.Slides for Python via .NET is a powerful Python library that allows you to create and manipulate PowerPoint PPT/PPTX without MS Office. Moreover, you can update existing presentations and convert them to other formats. You can install the library from PyPI using the following pip command.

> pip install aspose.slides

Create a PowerPoint PPT in Python

First, let’s create a PowerPoint presentation having an empty slide, which is added by default. The following steps show how to create a PowerPoint PPT in Python.

  • First, create an instance of the Presentation class.
  • Then, save PPT using Presentation.save(string, export.SaveFormat) method.

The following code sample shows how to create a PowerPoint PPTX file in Python.

Open a PowerPoint PPTX in Python

You can also open an existing presentation and update its content. To achieve this, you only need to provide the path of the presentation file in the Presentation constructor. The following code sample shows how to open an existing PPT/PPTX file in Python.

Python: Add a Slide to PowerPoint PPTX

Let’s now add a new slide in the presentation that we have created before. You can also add slides to an existing presentation in the same way by opening the presentation first (as shown in the previous section). The following are the steps to add a slide in the PPT/PPTX presentation in Python.

  • First, create a new (or load existing) presentation using Presentation class.
  • Then, get reference of the slides collection using Presentation.slides property.
  • Add slide using Presentation.slides.add_empty_slide(LayoutSlide) method.
  • Finally, save the presentation using Presentation.save(string, export.SaveFormat) method.

The following code sample shows how to add a slide in a PowerPoint PPT in Python.

Python: Create PowerPoint PPTX and Add Text

Once we have added a slide, we can proceed to add the content. First, let’s see how to add text to the presentation slides in Python.

  • Create a new (or load existing) PPT/PPTX using Presentation class.
  • Get reference of the slide from Presentation.slides collection.
  • Add a new auto shape in slide using Slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, int, int, int, int) method and get shape’s reference in an object.
  • Add a text frame to the shape using add_text_frame(string) method.
  • Access the default paragraph of the text frame.
  • Access the default portion of the paragraph.
  • Add text to the portion and save the presentation using Presentation.save(string, export.SaveFormat) method.

The following code sample shows how to add text to the slide in a PPT using Python.

The following is the screenshot of the slide after adding the text.

add text in powerpoint ppt in python

Read more about working with text in PowerPoint presentations.

Add Table in a PowerPoint PPTX in Python

Tables are an integral part of the documents and are widely used in PowerPoint presentations as well. Aspose.Slides for Python makes it quite easier for you to create a table in a presentation. So let’s see how to create a table in a PPT using Python.

  • Create a new (or load existing) PPT/PPTX using Presentation class.
  • Get reference of the slide from Presentation.slides collection.
  • Define arrays of columns with width and rows with height.
  • Create table using Slide.shapes.addTable() and get reference of the table in an object.
  • Loop through the cells to apply the formatting.
  • Add text to the cells using Table.rows[][].text_frame.text property.
  • Save the presentation using Presentation.save(string, export.SaveFormat) method.

The following code sample shows how to create tables in a PowerPoint PPTX in Python.

The following is the output of the code snippet above.

add table in PowerPoint ppt in Python

To explore other features related to table, visit creating and manipulating tables.

Python: Create Charts in a PowerPoint Presentations

The following are the steps to add a chart in a PowerPoint presentation in Python.

  • Create a new (or load existing) presentation using Presentation class.
  • Get reference of the slide from Presentation.slides collection.
  • Add a chart with the desired type using Slide.shapes.addChart(ChartType, single, single, single, single) method.
  • Access the chart data worksheet.
  • Clear all the default series and categories.
  • Add new series and categories.
  • Add new chart data for chart series.
  • Set fill color for chart series.
  • Add chart series labels.
  • Save the presentation as a PPTX file.

The following code sample shows how to add a chart in a PPT using Python.

The following screenshot shows the chart we have created using the code snippet above.

add chart in PowerPoint pptx in Python

Learn more about creating charts in PowerPoint presentations.

Create a PPT in Python - Add an Image

The following are the steps to add an image in a PowerPoint PPTX in Python.

  • Create a new (or load existing) presentation using Presentation class.
  • Get reference of the slide from Presentation.slides collection.
  • Open an image from file using open(string, string) method.
  • Add image into the image collection of presentation using Presentation.images.add_image() method.
  • Add image to the slide using Slide.shapes.add_picture_frame() method.
  • Save the presentation using Presentation.save(string, export.SaveFormat) method.

The following code sample shows how to add an image to a PowerPoint presentation in Python.

The following is the output of the code sample above.

add images in PowerPoint PPT in Python

Read more about working with images in PowerPoint presentations.

Python Library to Create PowerPoint PPTX - Get a Free License

You can use Aspose.Slides for Python via .NET without evaluation limitations by getting a free temporary license.

Conclusion

In this article, you have learned how to create PowerPoint PPT/PPTX from scratch in Python. We have demonstrated how to add slides, text, tables, charts, and images in PowerPoint PPT/PPTX presentations. Furthermore, you can explore other features of Aspose.Slides for Python using the documentation. Also, feel free to share your queries with us via our forum.

See Also