Create MS PowerPoint PPT/PPTX in Python

Create and modify PowerPoint ppt pptx in Python

MS PowerPoint is a feature-rich application that lets you create interactive presentations using charts, graphics, animations, and other elements. As a programmer, you might need to automate PowerPoint’s presentation manipulation features from within the Python applications. In this post, you will learn how to create PowerPoint PPT or PPTX presentations in Python. In addition, we will cover how to add slides, text, tables, images, and charts in the presentations programmatically.

Python Library to Create PowerPoint PPT

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 File 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.

Add a Slide to PowerPoint PPT in Python

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 presentation in Python.

Add Text to a PowerPoint PPT in Python

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.

Create Table in a PowerPoint PPT 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 presentation using Python.

The following is the output of the code snippet above.

add table in PowerPoint ppt in Python

Explore other features of creating and manipulating tables.

Create Chart in a PowerPoint PPT in Python

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.

Add an Image in PowerPoint PPTX in Python

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.

Get a Free License

You can use Aspose.Slides for Python via .NET without evaluation limitations by getting a 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 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