Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 3, 2022 03:55
Show Gist options
  • Save aspose-com-gists/9c6d62f894135aa1dbc332394953da8f to your computer and use it in GitHub Desktop.
Save aspose-com-gists/9c6d62f894135aa1dbc332394953da8f to your computer and use it in GitHub Desktop.
Create and Manipulate Tables in PowerPoint PPT/PPTX using Python
import aspose.slides as slides
import aspose.pydrawing as drawing
# Create a new presentation (to load an existing presentation, provide file's path in constructor)
with slides.Presentation() as pres:
# Access first slide
sld = pres.slides[0]
# Define columns with widths and rows with heights
dblCols = [50, 50, 50]
dblRows = [50, 30, 30, 30, 30]
# Add table shape to slide
tbl = sld.shapes.add_table(100, 50, dblCols, dblRows)
# Set border format for each cell
for row in range(len(tbl.rows)):
for cell in range(len(tbl.rows[row])):
# Add text
tbl.rows[row][cell].text_frame.text = "Cell_" + cell
# Set border
tbl.rows[row][cell].cell_format.border_top.fill_format.fill_type = slides.FillType.SOLID
tbl.rows[row][cell].cell_format.border_top.fill_format.solid_fill_color.color = drawing.Color.red
tbl.rows[row][cell].cell_format.border_top.width = 5
tbl.rows[row][cell].cell_format.border_bottom.fill_format.fill_type = slides.FillType.SOLID
tbl.rows[row][cell].cell_format.border_bottom.fill_format.solid_fill_color.color= drawing.Color.red
tbl.rows[row][cell].cell_format.border_bottom.width =5
tbl.rows[row][cell].cell_format.border_left.fill_format.fill_type = slides.FillType.SOLID
tbl.rows[row][cell].cell_format.border_left.fill_format.solid_fill_color.color =drawing.Color.red
tbl.rows[row][cell].cell_format.border_left.width = 5
tbl.rows[row][cell].cell_format.border_right.fill_format.fill_type = slides.FillType.SOLID
tbl.rows[row][cell].cell_format.border_right.fill_format.solid_fill_color.color = drawing.Color.red
tbl.rows[row][cell].cell_format.border_right.width = 5
# Merge cells 1 & 2 of row 1
tbl.merge_cells(tbl.rows[0][0], tbl.rows[1][1], False)
# Add text to the merged cell
tbl.rows[0][0].text_frame.text = "Merged Cells"
# Save PPTX to Disk
pres.save("table.pptx", slides.export.SaveFormat.PPTX)
# Load presentation
with slides.Presentation("table.pptx") as pres:
# Access the first slide
sld = pres.slides[0]
# Initialize null TableEx
tbl = None
# Iterate through the shapes and set a reference to the table found
for shp in sld.shapes:
if type(shp) is slides.Table:
tbl = shp
# Set the text of the first column of second row
tbl.rows[0][1].text_frame.text = "New"
# Save the PPTX to Disk
pres.save("table1_out.pptx", slides.export.SaveFormat.PPTX)
import aspose.slides as slides
# Create a presentation
with slides.Presentation() as presentation:
# Add table
someTable = presentation.slides[0].shapes.add_table(100, 100, [100, 50, 30], [30, 50, 30])
# Set table cells' font height
portionFormat = slides.PortionFormat()
portionFormat.font_height = 25
someTable.set_text_format(portionFormat)
# Set table cells' text alignment and right margin in one call
paragraphFormat = slides.ParagraphFormat()
paragraphFormat.alignment = slides.TextAlignment.RIGHT
paragraphFormat.margin_right = 20
someTable.set_text_format(paragraphFormat)
# Set table cells' text vertical type
textFrameFormat = slides.TextFrameFormat()
textFrameFormat.text_vertical_type = slides.TextVerticalType.VERTICAL
someTable.set_text_format(textFrameFormat)
# Save presentation
presentation.save("table-formatting.pptx", slides.export.SaveFormat.PPTX)
import aspose.slides as slides
# Create presentation
with slides.Presentation() as pres:
# Add table
table = pres.slides[0].shapes.add_table(100, 100, [100, 50, 30], [30, 50, 30])
print("Lock aspect ratio set: {0}".format(table.shape_lock.aspect_ratio_locked))
# Lock aspect ratio
table.shape_lock.aspect_ratio_locked = not table.shape_lock.aspect_ratio_locked
print("Lock aspect ratio set: {0}".format(table.shape_lock.aspect_ratio_locked))
# Save presentation
pres.save("pres-out.pptx", slides.export.SaveFormat.PPTX)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment