Read the complete article on how to create and edit tables in PowerPoint PPT/PPTX using Python: https://blog.aspose.com/2022/03/02/work-with-tables-in-powerpoint-ppt-in-python/
Last active
March 3, 2022 03:55
-
-
Save aspose-com-gists/9c6d62f894135aa1dbc332394953da8f to your computer and use it in GitHub Desktop.
Create and Manipulate Tables in PowerPoint PPT/PPTX using Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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