Python:在 PowerPoint PPT 中创建表格 您所在的位置:网站首页 如何在PPT中画表格 Python:在 PowerPoint PPT 中创建表格

Python:在 PowerPoint PPT 中创建表格

2024-07-12 23:08| 来源: 网络整理| 查看: 265

表格通常用于以行和列的形式组织数据。它们使查看、理解和分析数据变得更加容易。在各种情况下,您必须在 PowerPoint 演示文稿中插入表格。为了以编程方式完成此任务,本文介绍了如何使用 Python 在 PowerPoint PPT 或 PPTX 中创建表格。此外,您将学习如何访问、修改和格式化现有的 PowerPoint 表格。

用于创建和操作 PowerPoint 表格的 Python 库用 Python 在 PowerPoint PPT 中创建表格在 PPTX 中编辑表格格式化 PowerPoint 表格中的文本锁定PPT表格的纵横比用于创建和操作 PowerPoint 表格的 Python 库#

Aspose.Slides for Python 提供了一系列功能来创建、操作和转换 PowerPoint 和 OpenOffice 文档。我们将使用这个库在 PowerPoint 演示文稿中创建、编辑和操作表格。您可以使用以下命令从 PyPI 安装库。

> pip install aspose.slides 使用 Python 在 PowerPoint PPT 中创建表格#

以下是用 Python 在 PowerPoint PPT/PPTX 中创建表格的步骤。

首先,使用 Presentation 类加载或创建 PPT/PPTX 演示文稿。然后,获取要在其中添加表格的所需幻灯片的参考。之后,创建两个数组来分别定义列和行的宽度和高度。使用 ISlide.shapes.addtable() 方法在幻灯片上插入一个新表格并获取其引用。开始循环以遍历表的行。启动一个嵌套循环来遍历表格的单元格,并在每次迭代中执行以下操作。使用 Table.rows[row][cell].textframe.text 属性设置单元格的文本。如果需要,设置单元格的边框样式。最后,使用 Presentation.save(string, SaveFormat) 方法保存演示文稿。

以下代码示例演示如何在 PowerPoint 演示文稿中创建表格。

import aspose.slides as slides import aspose.pydrawing as drawing # 创建一个新的演示文稿(加载现有的演示文稿,在构造函数中提供文件的路径) with slides.Presentation() as pres: # 访问第一张幻灯片 sld = pres.slides[0] # 定义具有宽度的列和具有高度的行 dblCols = [50, 50, 50] dblRows = [50, 30, 30, 30, 30] # 将表格形状添加到幻灯片 tbl = sld.shapes.add_table(100, 50, dblCols, dblRows) # 为每个单元格设置边框格式 for row in range(len(tbl.rows)): for cell in range(len(tbl.rows[row])): # 添加文字 tbl.rows[row][cell].text_frame.text = "Cell_" + cell # 设置边框 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 # 合并第 1 行的单元格 1 和 2 tbl.merge_cells(tbl.rows[0][0], tbl.rows[1][1], False) # 添加文字 to the merged cell tbl.rows[0][0].text_frame.text = "Merged Cells" # 将 PPTX 保存到磁盘 pres.save("table.pptx", slides.export.SaveFormat.PPTX)

以下屏幕截图显示了我们使用上述代码创建的表。

使用 Python 在 PowerPoint PPT 中编辑表格#

您还可以通过从演示幻灯片访问现有表格来修改它。这是您可以访问 PowerPoint 表格并在 Python 中编辑其内容或外观的方式。

首先,使用 Presentation 类加载现有的 PowerPoint PPT/PPTX 文件。然后,获取所需幻灯片的引用到对象中。为表创建一个对象并使用 None 对其进行初始化。使用 ISlide.shapes 集合遍历幻灯片中的所有形状。过滤表格类型的形状。根据需要操作表格。最后,使用 Presentation.save(string, SaveFormat) 方法保存演示文稿。

以下代码示例展示了如何使用 Python 在 PowerPoint PPT 中编辑表格。

# 加载演示文稿 with slides.Presentation("table.pptx") as pres: # 访问第一张幻灯片 sld = pres.slides[0] # 初始化空表Ex tbl = None # 遍历形状并设置对找到的表的引用 for shp in sld.shapes: if type(shp) is slides.Table: tbl = shp # 设置第二行第一列的文本 tbl.rows[0][1].text_frame.text = "New" # 将 PPTX 保存到磁盘 pres.save("table1_out.pptx", slides.export.SaveFormat.PPTX) 用 Python 格式化 PowerPoint 表格中的文本#

Aspose.Slides for Python 还允许您将格式应用于表格内的文本。以下步骤显示了如何实现这一点。

首先,使用 Presentation 类加载现有的演示文稿。然后,获取所需幻灯片的引用到对象中。从幻灯片中获取所需表格的引用到对象中。使用 PortionFormat、ParagraphFormat 和 TextFrameFormat 对象设置格式。使用 Table.settextformat() 方法为表格分配格式。最后,使用 Presentation.save(string, SaveFormat) 方法保存演示文稿。

以下代码示例演示如何使用 Python 在 PowerPoint 中设置表格内的文本格式。

import aspose.slides as slides # 创建演示文稿 with slides.Presentation() as presentation: # 添加表 someTable = presentation.slides[0].shapes.add_table(100, 100, [100, 50, 30], [30, 50, 30]) # 设置表格单元格的字体高度 portionFormat = slides.PortionFormat() portionFormat.font_height = 25 someTable.set_text_format(portionFormat) # 一次调用设置表格单元格的文本对齐方式和右边距 paragraphFormat = slides.ParagraphFormat() paragraphFormat.alignment = slides.TextAlignment.RIGHT paragraphFormat.margin_right = 20 someTable.set_text_format(paragraphFormat) # 设置表格单元格的文本垂直类型 textFrameFormat = slides.TextFrameFormat() textFrameFormat.text_vertical_type = slides.TextVerticalType.VERTICAL someTable.set_text_format(textFrameFormat) # 保存演示文稿 presentation.save("table-formatting.pptx", slides.export.SaveFormat.PPTX) 在 Python 中锁定 PowerPoint 表格的纵横比#

您还可以使用 Python 锁定 PowerPoint 演示文稿中表格的纵横比,如以下步骤所示。

首先,使用 Presentation 类加载现有的演示文稿。然后,获取所需幻灯片的引用到对象中。创建表或将现有表的引用检索到对象中。使用 Table.shapelock.aspectratiolocked 属性锁定纵横比。最后,使用 Presentation.save(string, SaveFormat) 方法保存演示文稿。

下面的代码示例展示了如何在 PowerPoint PPTX 中锁定表格的纵横比。

import aspose.slides as slides # 创建演示文稿 with slides.Presentation() as pres: # 添加表 table = pres.slides[0].shapes.add_table(100, 100, [100, 50, 30], [30, 50, 30]) print("锁定纵横比 set: {0}".format(table.shape_lock.aspect_ratio_locked)) # 锁定纵横比 table.shape_lock.aspect_ratio_locked = not table.shape_lock.aspect_ratio_locked print("锁定纵横比 set: {0}".format(table.shape_lock.aspect_ratio_locked)) # 保存演示文稿 pres.save("pres-out.pptx", slides.export.SaveFormat.PPTX) 用于创建 PowerPoint 表格的 Python 库 - 获得免费许可证#

您可以通过获得免费的 临时许可证 来使用 Aspose.Slides for Python,而不受评估限制。

结论#

表格是文档的组成部分,用于组织数据。在本文中,您学习了如何在 PowerPoint PPT 和 Python PPTX 中创建表格。此外,您还了解了如何以编程方式访问和操作 PowerPoint 演示文稿中的现有表格。此外,您还可以访问 文档 以了解更多关于 Aspose.Slides for Python 的信息。此外,您可以通过我们的 论坛 提问。

也可以看看#在 Python 中创建 PowerPoint 文件在 Python 中将 PPTX 转换为 PDF在 Python 中将 PPT 转换为 PNG用 Python 在 PowerPoint PPT 中添加水印使用 Python 在 PowerPoint PPT 中应用 3D 效果

信息:使用 Aspose JPG to PPT 或 PNG to PPT 转换器,您可以从简单的图像生成 PowerPoint 演示文稿。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有