Create and Manipulate Tables in PowerPoint PPT using Python

Create and Manipulate Tables in PowerPoint Python

Tables are commonly used to organize the data in the form of rows and columns. They make it quite easier to view, understand and analyze the data. In various cases, you have to insert tables in your PowerPoint presentations. To accomplish that programmatically, this article covers how to create tables in PowerPoint PPT or PPTX using Python. Moreover, you will learn how to access, modify, and format the existing PowerPoint tables.

Python Library to Create and Manipulate PowerPoint Tables

Aspose.Slides for Python provides a bunch of features to create, manipulate and convert PowerPoint and OpenOffice documents. We will use this library to create, edit and manipulate tables in our PowerPoint presentations. You can install the library from PyPI using the following command.

> pip install aspose.slides

Create a Table in PowerPoint PPT using Python

The following are the steps to create a table in a PowerPoint PPT/PPTX in Python.

  • First, use Presentation class to load or create a PPT/PPTX presentation.
  • Then, get reference of the desired slide where you want to add the table.
  • After that, create two arrays to define the width and height of columns and rows, respectively.
  • Insert a new table on slide using ISlide.shapes.add_table() method and get its reference.
  • Start a loop to iterate through the rows of the table.
  • Start a nested loop to iterate through the cells of the table and perform the following operations in each iteration.
    • Set text of the cell using Table.rows[row][cell].text_frame.text property.
    • Set cell’s border style, if required.
  • Finally, save the presentation using Presentation.save(string, SaveFormat) method.

The following code sample shows how to create a table in a PowerPoint presentation.

The following screenshot shows the table that we have created using the above code.

Create a Table in PowerPoint PPT using Python

Edit a Table in a PowerPoint PPT using Python

You can also modify an existing table by accessing it from the presentation slide. This is how you can access a PowerPoint table and edit its content or appearance in Python.

  • First, load an existing PowerPoint PPT/PPTX file using Presentation class.
  • Then, get reference of the desired slide into an object.
  • Create an object for table and initialize it with None.
  • Iterate through all shapes in slide using ISlide.shapes collection.
  • Filter the shapes of type Table.
  • Manipulate the table as required.
  • Finally, save the presentation using Presentation.save(string, SaveFormat) method.

The following code sample shows how to edit tables in a PowerPoint PPT using Python.

Format Text in PowerPoint Tables using Python

Aspose.Slides for Python also allows you to apply the formatting to the text inside tables. The following steps show how you can achieve this.

  • First, load an existing presentation using Presentation class.
  • Then, get reference of the desired slide into an object.
  • Get the reference of the desired table from slide into an object.
  • Set formatting using PortionFormat, ParagraphFormat, and TextFrameFormat objects.
  • Assign formatting to the table using Table.set_text_format() method.
  • Finally, save the presentation using Presentation.save(string, SaveFormat) method.

The following code sample shows how to set the formatting of the text inside a table in PowerPoint using Python.

Lock Aspect Ratio of PowerPoint Tables in Python

You can also lock the aspect ratio of the tables in PowerPoint presentations using Python, as demonstrated in the following steps.

  • First, load an existing presentation using Presentation class.
  • Then, get reference of the desired slide into an object.
  • Create a table or retrieve the reference of an existing table into an object.
  • Lock aspect ratio using Table.shape_lock.aspect_ratio_locked property.
  • Finally, save the presentation using Presentation.save(string, SaveFormat) method.

The following code sample shows how to lock the aspect ratio of the table in PowerPoint PPTX.

Get a Free License

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

Info: Using Aspose JPG to PPT or PNG to PPT converter, you can generate PowerPoint presentations from simple images.

Conclusion

Tables are an integral part of the documents, which are used to organize the data. In this article, you have learned how to create tables in a PowerPoint PPT/PPTX in Python. Moreover, you have seen how to access and manipulate existing tables in PowerPoint presentations programmatically. Besides, you can also visit the documentation to explore more about Aspose.Slides for Python. Also, you can ask your questions via our forum.

See Also