在 PowerPoint 中,表格边框是指围绕表格及其单元格的可见线条。它们用于界定每个单元格的边界,使表格的结构和布局清晰可见。通过设置或修改表格边框,你可以自定义 PowerPoint 演示文稿中表格的外观。在本文中,我们将介绍如何使用 Python 和 Spire.Presentation for Python 给 PowerPoint 演示文稿中的表格设置边框和删除边框。
安装 Spire.Presentation for Python
本教程需要 Spire.Presentation for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.Presentation
如果您不确定如何安装,请参考此教程: 如何在 Windows 中安装 Spire.Presentation for Python
Python 设置 PowerPoint 表格边框
Spire.Presentation for Python 提供了 ITable.SetTableBorder() 方法,用于给 PowerPoint 中的特定表格设置边框。具体步骤如下:
- 创建一个 Presentation 类的对象。
- 使用 Presentation.Slides[] 属性获取演示文稿的第一张幻灯片。
- 使用 ISlide.Shapes.AppendTable() 方法向幻灯片添加一个表格。
- 使用 ITable.SetTableBorder() 方法为表格添加边框,并设置边框类型、宽度和颜色。
- 使用 Presentation.SaveToFile() 方法保存结果演示文稿。
- Python
from spire.presentation.common import *
from spire.presentation import *
# 创建 Presentation 对象
presentation = Presentation()
# 获取演示文稿的第一张幻灯片
slide = presentation.Slides[0]
# 指定表格的行数、列数和尺寸
widths = [100, 100, 100, 100, 100]
heights = [20, 20]
# 在第一张幻灯片上添加表格
table = slide.Shapes.AppendTable(100, 100, widths, heights)
# 为表格添加边框,并设置边框类型、宽度和颜色
table.SetTableBorder(TableBorderType.All, 1, Color.get_Blue())
# 保存结果演示文稿
presentation.SaveToFile("设置边框.pptx", FileFormat.Pptx2013)
presentation.Dispose()
Python 移除 PowerPoint 表格边框
要移除表格的边框,您需要遍历表格中的单元格,然后移除每个单元格的边框。具体步骤如下:
- 创建一个 Presentation 类的对象。
- 使用 Presentation.LoadFromFile() 方法加载一个 PowerPoint 演示文稿。
- 使用 Presentation.Slides[] 属性获取演示文稿中的特定幻灯片。
- 获取幻灯片上的表格。
- 遍历表格中的行和每行中的单元格。
- 通过将单元格的顶部、底部、左侧和右侧边框的填充类型设置为 none来移除每个单元格的边框。
- 使用 Presentation.SaveToFile() 方法保存结果演示文稿。
- Python
from spire.presentation.common import *
from spire.presentation import *
# 创建 Presentation 对象
presentation = Presentation()
# 加载 PowerPoint 演示文稿
presentation.LoadFromFile("设置边框.pptx")
# 获取第一张幻灯片
slide = presentation.Slides[0]
# 获取幻灯片上的表格
table = slide.Shapes[0] if isinstance(slide.Shapes[0], ITable) else None
# 遍历表格中的行和单元格
for row in table.TableRows:
for cell in row:
# 通过将单元格的顶部、底部、左侧和右侧边框的填充类型设置为none,从每个单元格中移除边框
cell.BorderTop.FillType = FillFormatType.none
cell.BorderBottom.FillType = FillFormatType.none
cell.BorderLeft.FillType = FillFormatType.none
cell.BorderRight.FillType = FillFormatType.none
# 保存结果演示文稿
presentation.SaveToFile("移除边框.pptx", FileFormat.Pptx2013)
presentation.Dispose()
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。