如何使用 Python 在 PPTX 中插入草稿水印

在本分步教程中,我们将详细说明如何使用 Python** 在 PPTX 中插入草图水印,而不依赖于 MS PowerPoint。您可以使用此应用程序在 Windows、Linux 或 macOS 内的任何 .NET Core 和 Python 支持环境中**使用 Python 在 PowerPoint 中添加草稿水印。

在 Python 中向 PowerPoint 添加机密水印的步骤

  1. 设置环境以安装 Aspose.Slides for Python 通过 .NET 以在演示文稿中插入水印文本
  2. 使用 Presentation 类对象创建新演示文稿或加载现有演示文稿以插入水印
  3. 在演示文稿中循环访问演示文稿 Master Slide/s
  4. 为母版幻灯片集合中的每张幻灯片添加带有机密水印文本的自选图形
  5. 格式化形状和文本属性并对形状应用锁定以保护水印
  6. 将带水印的演示文稿保存在磁盘上

按照上述步骤,您可以使用 Python* 在 PPTX 中插入一个*机密水印,该过程通过加载现有演示文稿或使用 Presentation 类的实例创建新演示文稿来开始。然后,您将遍历母版幻灯片集合中的每张幻灯片,并在其中添加草稿水印文本形状。最后,您将使用 auto_shape_lock 类实例公开的不同锁来保护形状,并将带水印的演示文稿保存在磁盘上。

在没有互操作的情况下使用 Python 在 PowerPoint 中添加草稿水印的代码

from os import system
import aspose.pydrawing as drawing
import aspose.slides as slides
filepath = "C://Slides//"
#Apply the licence for Aspose.Slides
slidesTextWatermarkLicense = slides.License()
slidesTextWatermarkLicense.set_license(filepath + "Conholdate.Total.Product.Family.lic")
# Generate an empty presentation using Presentation class object
with slides.Presentation() as textWatermarkPptxPresentation:
#Iterate through the master slide collection for adding a watermark text
for masterSlide in textWatermarkPptxPresentation.masters:
#Adding a shape to hold the watermark
pptxTextWatermark = masterSlide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE,
textWatermarkPptxPresentation.slide_size.size.width / 2 - 50,
textWatermarkPptxPresentation.slide_size.size.height / 2 - 50,
200, 50)
#Setting the rotation angle and fill type of the shape
pptxTextWatermark.rotation = 325
pptxTextWatermark.fill_format.fill_type = slides.FillType.NO_FILL
#Add the Text frame with watermark text
watermarkTextFrame = pptxTextWatermark.add_text_frame("Confidential Draft")
#set the textual properties of the watermark text
watermarkTextFormat = watermarkTextFrame.paragraphs[0].portions[0].portion_format
watermarkTextFormat.font_bold = slides.NullableBool.TRUE
watermarkTextFormat.font_italic = slides.NullableBool.TRUE
watermarkTextFormat.font_height = 20
watermarkTextFormat.fill_format.fill_type = slides.FillType.SOLID
watermarkTextFormat.fill_format.solid_fill_color.color = drawing.Color.red
#Lock the Pptx watermark shape and make it uneditable in PowerPoint
pptxTextWatermark.auto_shape_lock.text_locked=True
pptxTextWatermark.auto_shape_lock.select_locked = True
pptxTextWatermark.auto_shape_lock.position_locked = True
#Save the presentations with a text watermark on the disk
textWatermarkPptxPresentation.save(filepath + "PresentationWithWatermarkText.pptx", slides.export.SaveFormat.PPTX)
print("Finished")

此示例还可以用于 PPT 和 ODP 演示文稿格式,以在简单的 API 调用的帮助下将机密水印添加到 Python 中的 PowerPoint*。形状锁定功能是 API 提供的独特功能,MS PowerPoint 中没有。您可以通过实施锁定功能来保护您的演示文稿的知识产权,以保护水印形状并禁止任何人修改或删除它,即使在 PowerPoint 中也是如此。

在本主题中,我们学习了在 Python 中向 PowerPoint 添加草稿水印并保护演示文稿。如果您有兴趣了解如何在演示文稿中添加 HTML 内容,请参阅 如何使用 Python 将 HTML 插入到 PowerPoint 中 上的文章。

 简体中文