PowerPoint演示文稿作为展示创意、分享知识和表达观点的重要工具,被广泛应用于教育、商务汇报及个人项目展示等领域。然而,面对不同的分享场景与接收者需求,有时需要我们将PPT内容以图片形式保存与传播。这样能够避免软件兼容性的限制,确保信息接收者无需安装特定软件即可查看内容,还便于在网络社交平台、博客、电子邮件中快速分享与嵌入。而用Python代码可以高效地实现PowerPoint演示文稿到图片的批量转换,从而提升工作效率。
 文本将介绍如何使用Python实现PowerPoint演示文稿到图片的转换。
文章目录
- 将PowerPoint幻灯片转换为PNG图片
- 将PowerPoint幻灯片转换为图片并指定图片大小
- 将PowerPoint幻灯片转换为SVG图形文件
 
本文所使用的方法需要Spire.Presentation for Python,PyPI:pip install Spire.Presentation。
将PowerPoint幻灯片转换为PNG图片
我们可以使用库中的Presentation.Slides[]属性获取指定的幻灯片,然后使用ISlide.SaveAsImage()方法将幻灯片保存为图片流,之后再保存到图片文件即可。
 以下是详细操作步骤:
- 导入所需模块。
- 创建Presentation实例。
- 使用Presentation.LoadFromFile()方法从文件载入PowerPoint演示文稿。
- 遍历演示文稿中的幻灯片:
- 使用Presentation.Slides[]属性获取幻灯片。
- 使用ISlide.SaveAsImage()方法将幻灯片保存为图片流。
- 使用Stream.Save()方法将图片保存到文件。
- 释放资源。
代码示例
from spire.presentation import *
from spire.presentation.common import *
# 创建一个 Presentation 实例
presentation = Presentation()
# 加载一个演示文稿文件
presentation.LoadFromFile("示例.pptx")
# 遍历演示文稿中的幻灯片
for i in range(presentation.Slides.Count):
    # 获取当前幻灯片
    slide = presentation.Slides[i]
    # 将幻灯片保存为图像流
    image = slide.SaveAsImage()
    # 将图像保存到文件
    image.Save("output/PresentationToImage/Slide-" + str(i) + ".png")
# 释放资源
presentation.Dispose()
转换结果
 
将PowerPoint幻灯片转换为图片并指定图片大小
库中还提供了ISlide.SaveAsImageByWH()方法,以指定的宽度和高度,将幻灯片保存为图片流。
 以下是详细操作步骤:
- 导入所需模块。
- 创建Presentation实例。
- 使用Presentation.LoadFromFile()方法从文件载入PowerPoint演示文稿。
- 遍历演示文稿中的幻灯片:
- 使用Presentation.Slides[]属性获取幻灯片。
- 使用ISlide.SaveAsSvg()方法将幻灯片保存指定高度和宽度的图片流。
- 使用Stream.Save()方法将图片保存到文件。
- 释放资源。
代码示例:
from spire.presentation import *
from spire.presentation.common import *
# 创建一个 Presentation 实例
presentation = Presentation()
# 加载一个演示文稿文件
presentation.LoadFromFile("示例.pptx")
# 遍历所有幻灯片
for i in range(presentation.Slides.Count):
    # 获取幻灯片
    slide = presentation.Slides[i]
    # 将幻灯片保存为指定大小的图像流
    image = slide.SaveAsImageByWH(800, 600)
    # 将图像保存到文件
    image.Save("output/PresentationToImageWithSize/Slide" + str(i) + ".png")
# 释放资源
presentation.Dispose()
转换结果
 
将PowerPoint幻灯片转换为SVG图形文件
除了转换为普通的图片外,该库还提供一个ISlide.SaveToSvg()方法将幻灯片转换为SVG格式的图形。在转换之前,还以通过Presentation.IsNoteRetained属性设置是否在转换时保留幻灯片中的备注。
 以下是操作步骤:
- 导入所需模块。
- 创建Presentation实例。
- 使用Presentation.LoadFromFile()方法从文件载入PowerPoint演示文稿。
- 通过Presentation.IsNoteRetained属性设置是否在转换时保留幻灯片中的备注。
- 遍历演示文稿中的幻灯片:
- 使用Presentation.Slides[]属性获取幻灯片。
- 使用ISlide.SaveToSvg()方法将幻灯片保存为SVG图形流。
- 使用Stream.Save()方法将SVG图形保存到文件。
- 释放资源。
代码示例
from spire.presentation.common import *
from spire.presentation import *
# 创建一个 Presentation 实例
presentation = Presentation()
# 加载一个演示文稿文件
presentation.LoadFromFile("示例.pptx")
# 设置是否保留备注
presentation.IsNoteRetained = False
# 遍历幻灯片
for i in range(presentation.Slides.Count):
    # 获取幻灯片
    slide = presentation.Slides[i]
    # 将幻灯片保存为 SVG 流
    svg = slide.SaveToSVG()
    # 将 SVG 流保存到文件
    svg.Save("output/PresentationToSvg/Slide-" + str(i) + ".svg")
# 释放资源
presentation.Dispose()
转换结果
 
本文介绍了如何使用Python代码将PowerPoint演示文稿中的幻灯片保存到图片及SVG图形文件。
更多PowerPoint演示文稿处理技巧请前往Spire.Presentation for Python教程查看。
申请免费License



















