百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

使用 Python 在 PowerPoint 演示文稿中创建或提取表格

itomcoil 2025-05-30 15:12 24 浏览

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()

相关推荐

selenium(WEB自动化工具)

定义解释Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7,8,9,10,11),MozillaF...

开发利器丨如何使用ELK设计微服务中的日志收集方案?

【摘要】微服务各个组件的相关实践会涉及到工具,本文将会介绍微服务日常开发的一些利器,这些工具帮助我们构建更加健壮的微服务系统,并帮助排查解决微服务系统中的问题与性能瓶颈等。我们将重点介绍微服务架构中...

高并发系统设计:应对每秒数万QPS的架构策略

当面试官问及"如何应对每秒几万QPS(QueriesPerSecond)"时,大概率是想知道你对高并发系统设计的理解有多少。本文将深入探讨从基础设施到应用层面的解决方案。01、理解...

2025 年每个 JavaScript 开发者都应该了解的功能

大家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发。1.Iteratorhelpers开发者...

JavaScript Array 对象

Array对象Array对象用于在变量中存储多个值:varcars=["Saab","Volvo","BMW"];第一个数组元素的索引值为0,第二个索引值为1,以此类推。更多有...

Gemini 2.5编程全球霸榜,谷歌重回AI王座,神秘模型曝光,奥特曼迎战

刚刚,Gemini2.5Pro编程登顶,6美元性价比碾压Claude3.7Sonnet。不仅如此,谷歌还暗藏着更强的编程模型Dragontail,这次是要彻底翻盘了。谷歌,彻底打了一场漂亮的翻...

动力节点最新JavaScript教程(高级篇),深入学习JavaScript

JavaScript是一种运行在浏览器中的解释型编程语言,它的解释器被称为JavaScript引擎,是浏览器的一部分,JavaScript广泛用于浏览器客户端编程,通常JavaScript脚本是通过嵌...

一文看懂Kiro,其 Spec工作流秒杀Cursor,可移植至Claude Code

当Cursor的“即兴编程”开始拖累项目质量,AWS新晋IDEKiro以Spec工作流打出“先规范后编码”的系统工程思维:需求-设计-任务三件套一次生成,文档与代码同步落地,复杂项目不...

「晚安·好梦」努力只能及格,拼命才能优秀

欢迎光临,浏览之前点击上面的音乐放松一下心情吧!喜欢的话给小编一个关注呀!Effortscanonlypass,anddesperatelycanbeexcellent.努力只能及格...

JavaScript 中 some 与 every 方法的区别是什么?

大家好,很高兴又见面了,我是姜茶的编程笔记,我们一起学习前端相关领域技术,共同进步,也欢迎大家关注、点赞、收藏、转发,您的支持是我不断创作的动力在JavaScript中,Array.protot...

10个高效的Python爬虫框架,你用过几个?

小型爬虫需求,requests库+bs4库就能解决;大型爬虫数据,尤其涉及异步抓取、内容管理及后续扩展等功能时,就需要用到爬虫框架了。下面介绍了10个爬虫框架,大家可以学习使用!1.Scrapysc...

12个高效的Python爬虫框架,你用过几个?

实现爬虫技术的编程环境有很多种,Java、Python、C++等都可以用来爬虫。但很多人选择Python来写爬虫,为什么呢?因为Python确实很适合做爬虫,丰富的第三方库十分强大,简单几行代码便可实...

pip3 install pyspider报错问题解决

运行如下命令报错:>>>pip3installpyspider观察上面的报错问题,需要安装pycurl。是到这个网址:http://www.lfd.uci.edu/~gohlke...

PySpider框架的使用

PysiderPysider是一个国人用Python编写的、带有强大的WebUI的网络爬虫系统,它支持多种数据库、任务监控、项目管理、结果查看、URL去重等强大的功能。安装pip3inst...

「机器学习」神经网络的激活函数、并通过python实现激活函数

神经网络的激活函数、并通过python实现whatis激活函数感知机的网络结构如下:左图中,偏置b没有被画出来,如果要表示出b,可以像右图那样做。用数学式来表示感知机:上面这个数学式子可以被改写:...