网站首页 > 技术文章 正文
PowerPoint 中的表格是一种以结构化格式组织和呈现数据的方法,类似于 Excel 或 Word 等其他应用程序中表格的使用方式。它们提供了一种清晰简洁的方式来显示信息,使您的受众更容易消化和理解内容。
用于在 PowerPoint 演示文稿中创建或提取表格的 Python 库
要使用 Python 在 PowerPoint 演示文稿中创建或提取表格,我们可以使用 Spire.Presentation for Python 库。
Spire.Presentation for Python 是一个功能丰富且用户友好的库,支持在 Python 应用程序中创建、读取、编辑和转换 PowerPoint 演示文稿。使用此库,您可以对 PowerPoint 演示文稿执行各种操作,包括添加文本或图像、插入形状、创建表格和图表、添加或删除幻灯片、隐藏或显示幻灯片、合并或拆分演示文稿、加密或解密演示文稿、添加水印、插入视频和音频、添加评论等等。此外,您还可以将 PowerPoint 演示文稿转换为各种文件格式,例如 PDF、图像、HTML、SVG、ODP、OFD 和 XPS。
您可以使用以下 pip 命令从 PyPI 安装适用于 Python 的 Spire.Presentation:
pip install Spire.Presentation
有关安装的更多详细信息,您可以查看此官方文档:如何在 VS Code 中为 Python 安装 Spire.Presentation。
使用 Python 在 PowerPoint 演示文稿中创建表格
可以使用 ISlide.Shapes.AppendTable() 函数将表格添加到 PowerPoint 幻灯片中。添加表后,您可以使用 ITable[columnIndex, rowIndex] 用数据填充单元格。TextFrame.Text 属性。
下面是一个简单的示例,演示如何在 Python 中向 PowerPoint 幻灯片添加表格:
from spire.presentation.common import *
from spire.presentation import *
import math
# Create a new PowerPoint presentation
presentation = Presentation()
# If you want to load an existing presentation, add this line
# presentation.LoadFromFile("input_file_path")
# Get the first slide in the presentation
slide = presentation.Slides[0]
# Define the widths of columns in the table
column_widths = [110, 100, 150, 100]
# Define the heights of rows in the table
row_heights = [15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15]
# Calculate the total width of the table
total_width = sum(column_widths)
# Specify the x and y coordinates to add the table
x = math.trunc(presentation.SlideSize.Size.Width / float(2)) - (total_width / 2)
y = 100
# Add a table to the slide
table = slide.Shapes.AppendTable(x, y, column_widths, row_heights)
# Define the data for the table
table_data = [
["Employee", "Department", "Job Title", "Salary"],
["John Doe", "Sales", "Sales Manager", "$85,000"],
["Jane Smith", "Marketing", "Marketing Coordinator", "$52,000"],
["Bob Johnson", "IT", "Software Engineer", "$95,000"],
["Emily Davis", "HR", "HR Specialist", "$62,000"],
["Michael Wilson", "Finance", "Financial Analyst", "$78,000"],
["Sarah Lee", "Operations", "Operations Manager", "$90,000"],
["David Kim", "R&D", "Research Scientist", "$85,000"],
["Olivia Chen", "Marketing", "Marketing Specialist", "$58,000"],
["Tom Nguyen", "IT", "IT Support Technician", "$48,000"],
["Samantha Brown", "HR", "Recruitment Coordinator", "$55,000"]
]
# Populate the table with the data
for i, row in enumerate(table_data):
for j, cell_value in enumerate(row):
table[j, i].TextFrame.Text = cell_value
# Set the font and font size for the text in each cell
table[j,i].TextFrame.Paragraphs[0].TextRanges[0].LatinFont = TextFont("Arial")
table[j,i].TextFrame.Paragraphs[0].TextRanges[0].FontHeight = 12
# Center the text in each cell
table[j, i].TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center
# Apply a built-in table style to the table
table.StylePreset = TableStylePreset.LightStyle3Accent5
# Save the PowerPoint presentation to a file
presentation.SaveToFile("CreateTable.pptx", FileFormat.Pptx2010)
presentation.Dispose()
使用 Python 在 PowerPoint 幻灯片中插入表格
使用 Python 从 PowerPoint 演示文稿中提取表格
PowerPoint 幻灯片中的表格由 ITable 对象表示。因此,要提取表,您首先需要找到 ITable 类型的形状,然后遍历表中的单元格并使用 Cell.TextFrame.Text 属性获取单元格的数据。
下面是一个简单的示例,演示如何使用 Python 从 PowerPoint 演示文稿中提取表:
from spire.presentation.common import *
from spire.presentation import *
# Initialize an instance of the Presentation class
presentation = Presentation()
# Load a PowerPoint presentation containing tables
presentation.LoadFromFile("CreateTable.pptx")
# Create a list to store the extracted table data
table_data = []
# Traverse through the slides in the presentation
for slide in presentation.Slides:
# Traverse through the shapes on each slide
for shape in slide.Shapes:
# Check if the current shape is a table
if isinstance(shape, ITable):
table = shape
# Get the data of the cells in the table and append them to the list
table_data.extend([" | ".join(cell.TextFrame.Text.strip() for cell in row) for row in table.TableRows])
table_data.append("")
# Write the table data to a text file
with open("table.txt", "w", encoding="utf-8") as file:
file.write("\n".join(table_data))
presentation.Dispose()
猜你喜欢
- 2025-05-10 用Python写了一个上课点名系统(附源码)(自制考勤系统)
- 2025-05-10 第12天 | 12天搞定Python,word和pdf
- 2025-05-10 别人都教你DeepSeek做PPT ,我来个狠的,教你用它一键生成软件!
- 2025-05-10 一周学会用python操作excel、word、pdf吗!这篇教会你!拿走不谢
- 2025-05-10 2021年最全Python资料合集(视频+课件+源码资料)
- 2025-05-10 DeepSeek教你怎么制作PPT(怎么利用deepseek制作ppt)
- 2025-05-10 自己编写的PPT悬浮倒计时小工具,python带你玩嗨起来
- 2025-05-10 DeepSeek + Kimi 一键生成 PPT(deepseek + kimi = 一键生成ppt)
- 2025-05-10 太牛了!102个Python实战项目被我扒到了!建议收藏!
- 2025-05-10 绝了!一键用AI生成高颜值动态PPT(附详细步骤+Prompt)
- 258℃Python短文,Python中的嵌套条件语句(六)
- 258℃python笔记:for循环嵌套。end=""的作用,图形打印
- 257℃PythonNet:实现Python与.Net代码相互调用!
- 252℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 252℃Python实现字符串小写转大写并写入文件
- 108℃原来2025是完美的平方年,一起探索六种平方的算吧
- 91℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 83℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)