图片具有吸引观众并增强对内容理解的能力。通过在 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 向幻灯片添加图片
Spire.Presentation for Python 提供了 ISlide.Shapes.AppendEmbedImageByImageData() 方法,用于向特定幻灯片添加图片。具体步骤如下:
- 创建 Presentation 类的对象。
- 使用 Presentation.LoadFromFile() 方法加载一个 PowerPoint 演示文稿。
- 通过 Presentation.Slides[index] 属性获取特定幻灯片(根据索引)。
- 使用 Presentation.Images.AppendStream() 方法加载图片数据
- 使用 ISlide.Shapes.AppendEmbedImageByImageData() 方法将图片数据添加到幻灯片中。
- 使用 Presentation.SaveToFile() 方法保存结果演示文稿。
- Python
from spire.presentation.common import *
from spire.presentation import *
# 输入文件和输出文件的路径
inputFile = "示例文档.pptx"
outputFile = "添加图片到幻灯片.pptx"
# 创建一个 Presentation 对象并从输入文件加载 PPT 文档
presentation = Presentation()
presentation.LoadFromFile(inputFile)
# 获取第一张幻灯片对象
slide = presentation.Slides[0]
# 图片文件路径
imageFile = "Image.png"
# 通过流方式加载图片数据
stream = Stream(imageFile)
imageData = presentation.Images.AppendStream(stream)
# 获取图片宽度和高度
width = imageData.Width
height = imageData.Height
# 定义图片在幻灯片中的位置信息
rect1 = RectangleF.FromLTRB(20, 20, 20+width, 20+height)
# 在幻灯片上添加嵌入式图片
image = slide.Shapes.AppendEmbedImageByImageData(ShapeType.Rectangle, imageData, rect1)
# 设置图片的外框线为无填充效果
image.Line.FillType = FillFormatType.none
# 将更改后的 PPT 文档保存为输出文件,使用 Pptx2010 格式
presentation.SaveToFile(outputFile, FileFormat.Pptx2010)
# 释放 Presentation 对象所占用的资源
presentation.Dispose()
Python 向幻灯片母版添加图片
幻灯片母版是控制演示文稿中所有其他幻灯片的格式和样式的顶层幻灯片。当您对幻灯片母版进行更改,例如添加标志、修改背景或更改字体样式时,这些更改会自动应用于基于该幻灯片母版的所有幻灯片。如果您希望一张图片出现在所有幻灯片上,可以将其添加到幻灯片母版中。
Spire.Presentation for Python 提供了 IMasterSlide.Shapes.AppendEmbedImageByImageData() 方法,用于向幻灯片母版添加图片。具体步骤如下:
- 创建 Presentation 类的对象。
- 使用 Presentation.LoadFromFile() 方法加载一个 PowerPoint 演示文稿。
- 通过 Presentation.Masters[index] 属性获取特定的幻灯片母版(根据索引)。
- 使用 Presentation.Images.AppendStream() 方法加载图片数据
- 使用 IMasterSlide.Shapes.AppendEmbedImageByImageData() 方法将图片数据添加到幻灯片母版中。
- 使用 Presentation.SaveToFile() 方法保存结果演示文稿。
- Python
from spire.presentation.common import *
from spire.presentation import *
# 输入文件和输出文件的路径
inputFile = "输入文档.pptx"
outputFile = "添加图片到母板.pptx"
# 创建一个 Presentation 对象并从输入文件加载 PPT 文档
presentation = Presentation()
presentation.LoadFromFile(inputFile)
# 获取幻灯片母板对象
master = presentation.Masters[0]
# 图片文件路径
imageFile = "Logo.png"
# 通过流方式加载图片数据
stream = Stream(imageFile)
imageData = presentation.Images.AppendStream(stream)
# 定义图片在母板中的位置信息
rect1 = RectangleF.FromLTRB(40, 40, 40+imageData.Width, 40+imageData.Height)
# 在母板上添加嵌入式图片
image = master.Shapes.AppendEmbedImageByImageData(ShapeType.Rectangle, imageData, rect1)
# 设置图片的外框线为无填充效果
image.Line.FillType = FillFormatType.none
# 将更改后的 PPT 文档保存为输出文件,使用 Pptx2010 格式
presentation.SaveToFile(outputFile, FileFormat.Pptx2010)
# 释放 Presentation 对象所占用的资源
presentation.Dispose()
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。