A table is a structured way of organizing and presenting data in rows and columns. It usually consists of horizontal rows and vertical columns, and each intersection can contain text, numbers, or other types of data. By inserting a table into a presentation, users can create or display structured data on slides to make the content more organized. In addition, compared to text forms, tabular data can be more intuitive to show the differences between data, which helps readers understand more, thus enhancing the professionalism and readability of the presentation. This article is going to show how to use Spire.Presentation for Python to create or edit a table in a PowerPoint Presentation in Python programs.
- Insert Tables into PowerPoint Presentations in Python
- Edit Tables in PowerPoint Presentations in Python
Install Spire.Presentation for Python
This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.Presentation
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows
Insert Tables into PowerPoint Presentations in Python
Spire.Presentation for Python provides the Presentation.Slides[].Shapes.AppendTable(x: float, y: float, widths: List[float], heights: List[float]) method to add a table to a PowerPoint presentation. The detailed steps are as follows.
- Create an object of Presentation class.
- Load a sample presentation from disk using Presentation.LoadFromFile() method.
- Define the dimensions of the table.
- Add a new table to the sample presentation by calling Presentation.Slides[].Shapes.AppendTable(x: float, y: float, widths: List[float], heights: List[float]) method.
- Define the table data as a two-dimensional string array.
- Loop through the arrays and fill each cell of the table with these data by ITable[columnIndex, rowIndex].TextFrame.Text property.
- Set font name and font size for these data.
- Set the alignment of the first row in the table to center.
- Apply a built-in style to the table using ITable.StylePreset property.
- Save the result file using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * import math from spire.presentation import * inputFile = "C:/Users/Administrator/Desktop/Sample.pptx" outputFile = "C:/Users/Administrator/Desktop/CreateTable.pptx" #Create an object of Presentation class presentation = Presentation() #Load a sample presentation from disk presentation.LoadFromFile(inputFile) #Define the dimensions of the table widths = [100, 100, 150, 100, 100] heights = [15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15] #Add a new table to this presentation left = math.trunc(presentation.SlideSize.Size.Width / float(2)) - 275 table = presentation.Slides[0].Shapes.AppendTable(left, 90, widths, heights) #Define the table data as a two-dimensional string array dataStr = [["Name", "Capital", "Continent", "Area", "Population"], ["Venezuela", "Caracas", "South America", "912047", "19700000"], ["Bolivia", "La Paz", "South America", "1098575", "7300000"], ["Brazil", "Brasilia", "South America", "8511196", "150400000"], ["Canada", "Ottawa", "North America", "9976147", "26500000"], ["Chile", "Santiago", "South America", "756943", "13200000"], ["Colombia", "Bagota", "South America", "1138907", "33000000"], ["Cuba", "Havana", "North America", "114524", "10600000"], ["Ecuador", "Quito", "South America", "455502", "10600000"], ["Paraguay", "Asuncion", "South America", "406576", "4660000"], ["Peru", "Lima", "South America", "1285215", "21600000"], ["Jamaica", "Kingston", "North America", "11424", "2500000"], ["Mexico", "Mexico City", "North America", "1967180", "88600000"]] #Loop through the arrays for i in range(0, 13): for j in range(0, 5): #Fill each cell of the table with these data table[j,i].TextFrame.Text = dataStr[i][j] #Set font name and font size table[j,i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = TextFont("Arial") table[j,i].TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 12 #Set the alignment of the first row in the table to center for i in range(0, 5): table[i,0].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center #Apply a style to the table table.StylePreset = TableStylePreset.LightStyle3Accent1 #Save the result file presentation.SaveToFile(outputFile, FileFormat.Pptx2010) presentation.Dispose()
Edit Tables in PowerPoint Presentations in Python
You are also allowed to edit tables in the presentation as needed, such as replacing data, changing styles, highlighting data, and so on. Here are the detailed steps.
- Create an object of Presentation class.
- Load a sample presentation from disk using Presentation.LoadFromFile() method.
- Store the data used for replacement in a string.
- Loop through the shapes in the first slide, and determine if a certain shape is a table. If yes, convert it to an ITable object.
- Change the style of the table using ITable.StylePreset property.
- Replace the data in a specific cell range by using ITable[columnIndex, rowIndex].TextFrame.Text property.
- Highlight the new data using ITable[columnIndex, rowIndex].TextFrame.TextRange.HighlightColor.Color property.
- Save the result file using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * inputFile = "C:/Users/Administrator/Desktop/CreateTable.pptx" outputFile = "C:/Users/Administrator/Desktop/EditTable.pptx" #Create an object of Presentation class presentation = Presentation() #Load a sample presentation from disk presentation.LoadFromFile(inputFile) #Store the data used in replacement in a string strs = ["Germany", "Berlin", "Europe", "0152458", "20860000"] table = None #Loop through shapes in the first slide to get the table for shape in presentation.Slides[0].Shapes: if isinstance(shape, ITable): table = shape #Change the style of the table table.StylePreset = TableStylePreset.LightStyle1Accent2 for i, unusedItem in enumerate(table.ColumnsList): #Replace the data in a specific cell range table[i,2].TextFrame.Text = strs[i] #Highlight the new data table[i,2].TextFrame.TextRange.HighlightColor.Color = Color.get_Yellow() #Save the result file presentation.SaveToFile(outputFile, FileFormat.Pptx2013) presentation.Dispose()
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.