在这个简单而详尽的教程中,我们将重点介绍如何在未安装 PowerPoint 的情况下使用 Python** 在 PPTX 中添加图像水印。此应用程序可用于在 macOS、Windows 或 Linux 内的任何 .NET Core 和 Python 配置环境中使用 Python 添加 PowerPoint 水印背景图像**。
在Python中为PPTX演示文稿添加图像水印的步骤
- 建立环境以安装 Aspose.Slides for Python 通过 .NET 以在演示文稿中插入图像水印
- 使用 Presentation 类对象访问现有演示文稿或创建新演示文稿以添加水印图像
- 在演示图像集合中插入所需的水印图像
- 遍历演示文稿 Master Slide/s 集合中的每个形状
- 为母版幻灯片集合中的每张幻灯片插入带有所需图像的相框形状
- 在形状上应用锁定以保护图像水印
- 将带有图像水印的演示文稿保存在磁盘上
以上Python 中的步骤,使用非常简单的API 接口和几行代码在PPT 中添加图片水印。该过程通过加载现有演示文稿或创建新演示文稿以及从磁盘加载所需的水印图像来启动。然后迭代演示文稿母版幻灯片集合中的每张幻灯片,并创建使用水印图像的相框。最后,对每个形状应用不同的锁以保护它们不被访问或编辑。
使用Python在PPTX中添加图像水印的代码
import aspose.pydrawing as drawing | |
import aspose.slides as slides | |
filespath = "C://Slides//" | |
#Insert the licence for Aspose.Slides | |
slidesWatermarkLicense = slides.License() | |
slidesWatermarkLicense.set_license(filespath + "Conholdate.Total.Product.Family.lic") | |
# Generate an empty presentation using the Presentation class object | |
with slides.Presentation() as watermarkPptxPresentation: | |
with open(filespath + "sample.png", "rb") as binary_file: | |
#Read the whole file at once | |
imageFileData = binary_file.read() | |
#Insert the image insdie the images collection of the presentation | |
imageForSlide = watermarkPptxPresentation.images.add_image(imageFileData) | |
#Access the master slides collection for adding a watermark image | |
for masterSlide in watermarkPptxPresentation.masters: | |
#Adding a Ppt watermark shape for logo image | |
pptxWatermarkFrame = slideForPng.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 200, 200, 200, 50, imageForSlide) | |
#Set the rotation angle of the shape | |
pptxWatermarkFrame.rotation = 325 | |
#Lock Pptx watermark image shape for protection in PowerPoint | |
pptxWatermarkFrame.shape_lock.size_locked = True | |
pptxWatermarkFrame.shape_lock.select_locked = True | |
pptxWatermarkFrame.shape_lock.position_locked = True | |
# Save the presentations with the watermark on the disk | |
watermarkPptxPresentation.save(filespath + "PresentationWithWatermarkmage.pptx", slides.export.SaveFormat.PPTX) | |
print("Done") |
为了在 PPT 中添加水印图像,以上示例有效地使用了基于 Python 的 API。已使用 shape_lock 类,它提供不同的形状锁,包括锁定选择、调整大小和更改位置,以防止即使在 PowerPoint 中也可以访问形状。
在本主题中,我们学习了使用 Python 在 PPTX 中放入水印图像,并将知识产权嵌入到演示文稿中。如果您有兴趣在演示文稿中添加基于文本的水印,请参阅 如何使用Python在PPTX中插入草稿水印 上的文章。