powerpoint 演示文稿以有条理且富有吸引力的方式呈现信息著称。为了更好地组织管理幻灯片,powerpoint 提供了将幻灯片分组为节的功能。这一功能可以明显简化大型演示文稿的导航与管理。在本文中,我们将演示如何使用 spire.presentation for python 在 python 中管理 powerpoint 节中的幻灯片。具体来说,我们将介绍如何在节中添加、检索、重排和删除幻灯片。
- python 在 powerpoint 节中插入幻灯片
- python 从 powerpoint 节中获取幻灯片
- python 在 powerpoint 节中重排幻灯片
- 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,你可以通过 section.insert() 方法快速将幻灯片插入到指定节中。具体步骤如下:
- 创建一个 presentation 类的实例。
- 通过 presentation.loadfromfile() 方法,加载一个 powerpoint 演示文稿。
- 使用 presentation.sectionlist(index) 属性,通过其索引(从 0 开始)获取指定的节。
- 在演示文稿中添加一页新的幻灯片,然后使用 section.insert() 方法将其插入到指定节中。
- 从演示文稿中移除刚才添加的幻灯片。
- 通过 presentation.savetofile() 方法保存结果文件。
- python
from spire.presentation import *
# 创建一个 presentation 类的实例
presentation = presentation()
# 加载一个 powerpoint 演示文稿
presentation.loadfromfile("e:/administrator/python1/input/幻灯片1.pptx")
# 获取第一个节
first_section = presentation.sectionlist[0]
# 在演示文稿中添加一个新幻灯片并插入到第一节的开头
slide = presentation.slides.append()
first_section.insert(0, slide)
# 从演示文稿中删除添加的幻灯片
presentation.slides.remove(slide)
# 保存修改后的演示文稿
presentation.savetofile("e:/administrator/python1/output/在节中插入幻灯片.pptx", fileformat.pptx2016)
# 释放资源
presentation.dispose()
python 从 powerpoint 节中获取幻灯片
从指定节中提取幻灯片可以帮你专注于较小的一组幻灯片,用于重新排序或者应用自定义格式等任务。使用 spire.presentation for python 提供的 section.getslides() 方法,你可以轻松获取指定节的所有幻灯片。下面是具体的操作步骤。
- 创建一个 presentation 类的对象。
- 通过 presentation.loadfromfile() 方法,加载一个 powerpoint 演示文稿。
- 利用 presentation.sectionlist(index) 属性,通过其索引(从 0 开始)获取指定的节。
- 使用 section.getslides() 方法,获取该节的所有幻灯片。
- 遍历获取的幻灯片,同时取得每张幻灯片的编号(从 1 开始)。
- python
from spire.presentation import *
# 创建一个 presentation 类的实例
presentation = presentation()
# 加载一个 powerpoint 演示文稿
presentation.loadfromfile("e:/administrator/python1/input/幻灯片1.pptx")
# 获取第三节的幻灯片
section = presentation.sectionlist[2]
slides = section.getslides()
output_content = "该节中的幻灯片编号为:\n"
# 获取该节中的每张幻灯片的幻灯片编号
for slide in slides:
output_content = str(slide.slidenumber) " "
# 将幻灯片编号保存到文本文件中
with open("e:/administrator/python1/output/幻灯片编号.txt", "w", encoding="utf-8") as file:
file.write(output_content)
python 在 powerpoint 节中重排幻灯片顺序
重新排列幻灯片的顺序非常重要,它可以确保相关内容按正确的顺序展示。通过 spire.presentation for python 提供的 section.move() 方法,你可以在节中将一张幻灯片移动到新的位置。下面就是详细的步骤。
- 创建一个 presentation 类的对象。
- 使用 presentation.loadfromfile() 方法,加载 powerpoint 演示文稿。
- 利用 presentation.sectionlist(index) 属性,通过其索引(从 0 开始)获取指定的节。
- 利用 section.move() 方法,将节内的幻灯片移动到另一个位置。
- 通过 presentation.savetofile() 方法,保存更新后的演示文稿。
- python
from spire.presentation import *
# 创建一个 presentation 类的实例
presentation = presentation()
# 加载一个 powerpoint 演示文稿
presentation.loadfromfile("e:/administrator/python1/input/幻灯片1.pptx")
# 获取演示文稿的第二节
section = presentation.sectionlist[1]
# 获取该节中的幻灯片
slides = section.getslides()
# 将节中的第一张幻灯片移动到指定位置
section.move(2, slides[0])
# 保存修改后的演示文稿
presentation.savetofile("e:/administrator/python1/output/重排幻灯片.pptx", fileformat.pptx2016)
# 释放资源
presentation.dispose()
python 从 powerpoint 节中移除幻灯片
从节中移除幻灯片可以简化你的演示文稿,尤其是当部分幻灯片过时或不重要的时候。在 spire.presentation for python 的帮助下,你可以使用 section.removeat() 或 section.removerange() 方法从节中轻松移除单张或多张幻灯片。一起来看看具体的操作步骤吧。
- 实例化一个 presentation 类的对象。
- 使用 presentation.loadfromfile() 方法,加载一个 powerpoint 演示文稿。
- 通过 presentation.sectionlist(index) 属性,通过其索引(从 0 开始)获取指定的节。
- 使用 section.removeat() 方法从节中移除单张幻灯片,或通过 section.removerange() 方法移除多张幻灯片。
- 利用 presentation.savetofile() 方法,保存修改后的文件。
- python
from spire.presentation import *
# 创建一个 presentation 类的实例
presentation = presentation()
# 加载一个 powerpoint 演示文稿
presentation.loadfromfile("e:/administrator/python1/input/幻灯片1.pptx")
# 获取演示文稿的第二节
section = presentation.sectionlist[1]
# 从节中删除第一张幻灯片
section.removeat(0)
# 或从节中删除多张幻灯片
# section.removerange(0, 2)
# 保存修改后的演示文稿
presentation.savetofile("e:/administrator/python1/output/在节中移除幻灯片.pptx", fileformat.pptx2016)
# 释放资源
presentation.dispose()
申请临时 license
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用javascript。获取有效期 30 天的临时许可证。