Manage Presentation Tables with Python
Overview
A table in PowerPoint is an efficient way to present information. Information arranged in a grid of cells (rows and columns) is straightforward and easy to understand.
Aspose.Slides provides the Table class, the Cell class, and other related types to help you create, update, and manage tables in any presentation.
Create Tables from Scratch
This section shows how to create a table from scratch in Aspose.Slides by adding a table shape to a slide, defining its rows and columns, and setting precise sizes. You’ll also see how to populate cells with text, adjust alignment and borders, and customize the table’s appearance.
- Create an instance of the Presentation class.
- Get a reference to a slide by its index.
- Define an array of column widths.
- Define an array of row heights.
- Add a Table to the slide.
- Iterate over each Cell and format its top, bottom, right, and left borders.
- Merge the first two cells in the table’s first row.
- Access the TextFrame of a Cell.
- Add text to the TextFrame.
- Save the modified presentation.
The following Python example shows how to create a table in a presentation:
import aspose.pydrawing as draw
import aspose.slides as slides
# Instantiate the Presentation class that represents a presentation file.
with slides.Presentation() as presentation:
# Access the first slide.
slide = presentation.slides[0]
# Define column widths and row heights.
column_widths = [50, 50, 50]
row_heights = [50, 30, 30, 30, 30]
# Add a table shape to the slide.
table = slide.shapes.add_table(100, 50, column_widths, row_heights)
# Set the border format for each cell.
for row in table.rows:
for cell in row:
cell.cell_format.border_top.fill_format.fill_type = slides.FillType.SOLID
cell.cell_format.border_top.fill_format.solid_fill_color.color = draw.Color.red
cell.cell_format.border_top.width = 5
cell.cell_format.border_bottom.fill_format.fill_type = slides.FillType.SOLID
cell.cell_format.border_bottom.fill_format.solid_fill_color.color= draw.Color.red
cell.cell_format.border_bottom.width = 5
cell.cell_format.border_left.fill_format.fill_type = slides.FillType.SOLID
cell.cell_format.border_left.fill_format.solid_fill_color.color =draw.Color.red
cell.cell_format.border_left.width = 5
cell.cell_format.border_right.fill_format.fill_type = slides.FillType.SOLID
cell.cell_format.border_right.fill_format.solid_fill_color.color = draw.Color.red
cell.cell_format.border_right.width = 5
# Merge cells from (row 0, col 0) to (row 1, col 1).
table.merge_cells(table.rows[0][0], table.rows[1][1], False)
# Add text to the merged cell.
table.rows[0][0].text_frame.text = "Merged Cells"
# Save the presentation to disk.
presentation.save("table.pptx", slides.export.SaveFormat.PPTX)
Numbering in Standard Tables
In a standard table, cell numbering is straightforward and zero-based. The first cell in a table is indexed as (0, 0) (column 0, row 0).
For example, in a table with 4 columns and 4 rows, the cells are numbered as follows:
(0, 0) | (1, 0) | (2, 0) | (3, 0) |
---|---|---|---|
(0, 1) | (1, 1) | (2, 1) | (3, 1) |
(0, 2) | (1, 2) | (2, 2) | (3, 2) |
(0, 3) | (1, 3) | (2, 3) | (3, 3) |
The following Python example shows how to reference cells using this zero-based numbering:
for row_index in range(len(table.rows)):
for column_index in range(len(table.rows[row_index])):
cell = table.rows[row_index][column_index]
cell.text_frame.text = f"({column_index}, {row_index})"
Access an Existing Table
This section explains how to locate and work with an existing table in a presentation using Aspose.Slides. You’ll learn how to find the table on a slide, access its rows, columns, and cells, and update content or formatting.
- Create an instance of the Presentation class.
- Get a reference to the slide that contains the table by its index.
- Iterate through all Shape objects until you find the table.
- Use the Table object to work with the table.
- Save the modified presentation.
alternative_text
property.
The following Python example shows how to access and work with an existing table:
import aspose.pydrawing as draw
import aspose.slides as slides
# Instantiate the Presentation class to load a PPTX file.
with slides.Presentation("sample.pptx") as presentation:
# Access the first slide.
slide = presentation.slides[0]
table = None
# Iterate through shapes and reference the first table found.
for shape in slide.shapes:
if isinstance(shape, slides.Table):
table = shape
break
# Set the text of the first cell in the first row.
if table is not None:
table.rows[0][0].text_frame.text = "Found"
# Save the modified presentation to disk.
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Align Text in Tables
This section shows how to control text alignment inside table cells using Aspose.Slides. You’ll learn to set horizontal and vertical alignment for cells to keep your content clear and consistent.
- Create an instance of the Presentation class.
- Get a reference to the slide by its index.
- Add a Table object to the slide.
- Access a Cell object from the table.
- Align the text vertically.
- Save the modified presentation.
The following Python example shows how to align the text in a table:
import aspose.pydrawing as draw
import aspose.slides as slides
# Create an instance of the Presentation class.
with slides.Presentation() as presentation:
# Access the first slide.
slide = presentation.slides[0]
# Define column widths and row heights.
column_widths = [40, 120, 120, 120]
row_heights = [100, 100, 100, 100]
# Add a table shape to the slide.
table = slide.shapes.add_table(100, 50, column_widths, row_heights)
table.rows[0][0].text_frame.text = "Numbers"
table.rows[1][0].text_frame.text = "10"
table.rows[2][0].text_frame.text = "20"
table.rows[3][0].text_frame.text = "30"
# Center the text and set vertical orientation.
cell = table.rows[0][0]
cell.text_anchor_type = slides.TextAnchorType.CENTER
cell.text_vertical_type = slides.TextVerticalType.VERTICAL270
# Save the presentation to disk.
presentation.save("aligned_cell.pptx", slides.export.SaveFormat.PPTX)
Set Text Formatting at the Table Level
This section shows how to apply text formatting at the table level in Aspose.Slides so every cell inherits a consistent, unified style. You’ll learn to set font sizes, alignments, and margins globally.
- Create an instance of the Presentation class.
- Get a reference to the slide by its index.
- Add a Table to the slide.
- Set the font size (font height) for the text.
- Set paragraph alignment and margins.
- Set the vertical text orientation.
- Save the modified presentation.
The following Python example shows how to apply your preferred formatting options to text in a table:
import aspose.pydrawing as draw
import aspose.slides as slides
# Creates an instance of the Presentation class
with slides.Presentation() as presentation:
slide = presentation.slides[0]
table = slide.shapes.add_table(20, 20, [100, 50, 30], [30, 50, 30])
# Set the font size for all table cells.
portion_format = slides.PortionFormat()
portion_format.font_height = 25
table.set_text_format(portion_format)
# Set right-aligned text and a right margin for all table cells.
paragraph_format = slides.ParagraphFormat()
paragraph_format.alignment = slides.TextAlignment.RIGHT
paragraph_format.margin_right = 20
table.set_text_format(paragraph_format)
# Set the vertical text orientation for all table cells.
text_frame_format = slides.TextFrameFormat()
text_frame_format.text_vertical_type = slides.TextVerticalType.VERTICAL
table.set_text_format(text_frame_format)
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Apply Built-In Table Styles
Aspose.Slides lets you format tables using predefined styles directly in code. The example demonstrates creating a table, applying a built-in style, and saving the result—an efficient way to ensure consistent, professional formatting.
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
table = slide.shapes.add_table(10, 10, [100, 150], [5, 5, 5])
table.style_preset = slides.TableStylePreset.DARK_STYLE1
presentation.save("table.pptx", slides.export.SaveFormat.PPTX)
Lock Aspect Ratio of Tables
The aspect ratio of a shape is the ratio of its dimensions. Aspose.Slides provides the aspect_ratio_locked
property, which allows you to lock the aspect ratio for tables and other shapes.
The following Python example shows how to lock the aspect ratio for a table:
import aspose.pydrawing as draw
import aspose.slides as slides
with slides.Presentation() as presentation:
slide = presentation.slides[0]
table = slide.shapes.add_table(20, 20, [100, 50, 30], [30, 50, 30])
print(f"Lock aspect ratio set: {table.shape_lock.aspect_ratio_locked}")
table.shape_lock.aspect_ratio_locked = not table.shape_lock.aspect_ratio_locked
print(f"Lock aspect ratio set: {table.shape_lock.aspect_ratio_locked}")
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)