如何使用 Python 在 PowerPoint 中创建表格

这篇简单的文章指导如何使用 Python 在 PowerPoint 中创建表格。它涵盖了设置环境的所有信息、在表格中插入和填充数据的分步过程,以及说明如何使用 Python 在幻灯片中插入表格的工作示例代码。它还介绍了如何格式化表格单元格内的文本以及如何以 PPTPPTX 格式将演示文稿保存在磁盘上。

使用 Python 在 PowerPoint 中创建表格的步骤

  1. 在您的应用程序中配置 通过 .NET 使用 Aspose.Slides for Python 的环境 以添加表格
  2. 使用 Presentation 类生成一个新的空白演示文稿并访问其第一张默认幻灯片
  3. 使用 add_table() 方法在具有定义的列宽和行高的幻灯片中插入表格
  4. 遍历新添加的表格中的每一行及其各自的单元格
  5. 在每个单元格中设置示例文本及其与字体相关的属性
  6. 将演示文稿以 PPTX 格式的表格保存在磁盘上

上述步骤解释了如何使用 Python 在 PowerPoint 中制作表格,因此在第一步中,我们将创建一个默认演示文稿并访问其第一张幻灯片。在后续步骤中,我们将通过提供表格的位置坐标以及列宽和行高来添加表格。在最后的步骤中,我们将遍历表格中的每个单元格以设置文本以及相应的格式,然后再将结果演示文稿保存在磁盘上。

使用 Python 在 PowerPoint 中添加表格的代码

import aspose.pydrawing as draw
import aspose.slides as slides
# Path to the license file directory
filepath = "Y://Documents//KnowledgeBase//TestData//"
# Load the license in your application for creating the table
slidesTableLicense = slides.License()
slidesTableLicense.set_license(filepath + "Conholdate.Total.Product.Family.lic")
# Instantiate the Presentation object to add the table
with slides.Presentation() as presentationTable:
# Access the first default slide
slide = presentationTable.slides[0]
# Define the columns widths and rows heights
dblColsWidth = [50, 50, 50]
dblRowsHeight = [50, 30, 30, 30, 30]
# Insert the table shape to slide
table = slide.shapes.add_table(100, 50, dblColsWidth, dblRowsHeight)
# Set the border format for each cell
for rowIndex in range(len(table.rows)):
for cellIndex in range(len(table.rows[rowIndex])):
table.rows[rowIndex][cellIndex].cell_format.border_top.fill_format.fill_type = slides.FillType.SOLID
table.rows[rowIndex][cellIndex].cell_format.border_top.fill_format.solid_fill_color.color = draw.Color.red
table.rows[rowIndex][cellIndex].cell_format.border_top.width = 5
table.rows[rowIndex][cellIndex].cell_format.border_bottom.fill_format.fill_type = slides.FillType.SOLID
table.rows[rowIndex][cellIndex].cell_format.border_bottom.fill_format.solid_fill_color.color= draw.Color.red
table.rows[rowIndex][cellIndex].cell_format.border_bottom.width =5
table.rows[rowIndex][cellIndex].cell_format.border_left.fill_format.fill_type = slides.FillType.SOLID
table.rows[rowIndex][cellIndex].cell_format.border_left.fill_format.solid_fill_color.color =draw.Color.red
table.rows[rowIndex][cellIndex].cell_format.border_left.width = 5
table.rows[rowIndex][cellIndex].cell_format.border_right.fill_format.fill_type = slides.FillType.SOLID
table.rows[rowIndex][cellIndex].cell_format.border_right.fill_format.solid_fill_col or.color = draw.Color.red
table.rows[rowIndex][cellIndex].cell_format.border_right.width = 5
# Merge the cells 1 and 2 of row 1
table.merge_cells(table.rows[0][0], table.rows[1][1], False)
# Add the text inside the merged cell
table.rows[0][0].text_frame.text = "Merged Table Cells"
presentationTable.save(filepath + "NewPresentationWithTable.pptx", slides.export.SaveFormat.PPTX)
print("Done")

此示例涉及如何使用 Python 在 PowerPoint 中创建表格,其中 Table 类实例用于插入具有行和列集合的表格。 TextFrame 类对象设置段落文本的文本、字体高度和项目符号类型。您还可以使用其他相关属性,例如突出显示文本、设置填充格式、添加或删除字段以及设置突出显示颜色等等。

本主题解释了如何使用 Python 在演示文稿中插入表格。如果您有兴趣了解如何添加水印图像来保护演示文稿,请参阅 如何使用Python在PPTX中添加图片水印 上的文章。

 简体中文