重新排列 PowerPoint 文档的幻灯片顺序是一项简单但重要的技能。无论是需要更改要点的顺序,将相关幻灯片分组,还是移动幻灯片的位置,高效重组幻灯片都能帮您创造更连贯和具有影响力的演示效果。
在接下来的段落中,您将学习如何使用 Spire.Presentation for Python 在 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 幻灯片顺序
要在 PowerPoint 文档中重新排列幻灯片的顺序,您需要创建两个 Presentation 对象:一个用来加载原始文档,另一个用来创建新文档。将原始文档中的幻灯片按所需顺序复制到新文档中,就能轻松地重新排列幻灯片顺序。
利用 Python 来重新排列 PowerPoint 文件中幻灯片顺序的详细步骤如下:
- 创建一个 Presentation 对象。
- 通过 Presentation.LoadFromFile() 方法加载 PowerPoint 文档。
- 将幻灯片顺序整理为列表。
- 创建另一个 Presentation 对象用以新建演示文稿。
- 使用 Presentation.Slides.AppendBySlide() 方法将原文档中的幻灯片按指定顺序添加到新演示文稿中。
- 通过 Presentation.SaveToFile() 方法将新演示文稿保存为 PPTX 文件。
- Python
from spire.presentation import *
from spire.presentation.common import *
# 创建一个 Presentation 对象
presentation = Presentation()
# 加载 PowerPoint 文件
presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx")
# 将幻灯片顺序整理为列表
newSlideOrder = [4,2,1,3]
# 创建另一个 Presentation 对象
new_presentation = Presentation()
# 删除默认幻灯片
new_presentation.Slides.RemoveAt(0)
# 遍历列表
for i in range(len(newSlideOrder)):
# 将原文件的幻灯片按新顺序添加到新 PowerPoint 文档中
new_presentation.Slides.AppendBySlide(presentation.Slides[newSlideOrder[i] - 1])
# 保存新的演示文稿
new_presentation.SaveToFile("output/NewOrder.pptx", FileFormat.Pptx2019)
# 释放资源
presentation.Dispose()
new_presentation.Dispose()
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。