在PowerPoint演示文稿中插入文本水印是保护版权并确保文档内容真实性的有效方式。利用Python,开发者通过简单的代码添加水印到PowerPoint幻灯片中,进行批量处理,允许精确控制水印的位置和外观,便于集成到更大的应用程序中。本文将演示如何使用Python插入文字水印到PowerPoint演示文稿。

  • 用Python添加文字水印到演示文稿
  • 用Python插入重复文字水印到演示文稿
  • 用Python添加图片水印到演示文稿
  • 用Python添加重复图片水印到演示文稿

本文所使用的方法需要用到Spire.Presentation for Python,PyPI:pip install spire.presentation

申请免费License

用Python添加文字水印到演示文稿

我们可以通过在幻灯片中添加带有选中保护的透明文本框,并在其中插入水印文字,来实现在PowerPoint演示文稿文字水印的添加。以下是操作步骤:

  1. 导入必要的类:Presentation, FileFormat, RectangleF, ShapeType, FillFormatType, Color
  2. 定义输入输出文件路径:input_file, output_file
  3. 加载PPT文档:Presentation(), LoadFromFile()
  4. 计算水印位置:SlideSize.Size.Width, SlideSize.Size.Height
  5. 添加矩形水印:Shapes.AppendShape(), RectangleF()
  6. 设置矩形样式:FillType, LineColor.Color, Rotation, SelectionProtection, Line.FillType
  7. 添加文本至矩形:TextFrame.Text
  8. 设置文本样式:FillType, SolidColor.Color, FontHeight
  9. 保存与关闭文档:SaveToFile(), Dispose()

代码示例

from spire.presentation import Presentation, FileFormat, RectangleF, ShapeType, FillFormatType, Color

# 定义输入输出文件路径
input_file = "Sample.pptx"
output_file = "output/SingleTextWatermark.pptx"

# 创建并加载PPT文档
presentation = Presentation()
presentation.LoadFromFile(input_file)

# 计算水印位置
slide_width = presentation.SlideSize.Size.Width
slide_height = presentation.SlideSize.Size.Height
watermark_width = 336.4
watermark_height = 110.8
left = (slide_width - watermark_width) / 2
top = (slide_height - watermark_height) / 2

# 添加矩形形状作为水印
rect = RectangleF(left, top, watermark_width, watermark_height)
shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, rect)

# 设置矩形样式
shape.Fill.FillType = FillFormatType.none
shape.ShapeStyle.LineColor.Color = Color.get_White()
shape.Rotation = -45
shape.Locking.SelectionProtection = True
shape.Line.FillType = FillFormatType.none

# 添加文本到矩形
shape.TextFrame.Text = "Watermark"
text_range = shape.TextFrame.TextRange

# 设置文本样式
text_range.Fill.FillType = FillFormatType.Solid
text_range.Fill.SolidColor.Color = Color.get_Blue()
text_range.FontHeight = 50

# 保存并关闭文档
presentation.SaveToFile(output_file, FileFormat.Pptx2010)
presentation.Dispose()

结果
Python添加文字水印到PowerPoint演示文稿

用Python插入重复文字水印到演示文稿

除了插入单一文字水印外,我们还可以通过在PowerPoint幻灯片中,以指定间隔重复添加水印文字来实现多行文字水印的插入。以下是代码示例:

from spire.presentation import Presentation, FileFormat, RectangleF, ShapeType, FillFormatType, Color

# 定义输入输出文件路径
input_file = "Sample.pptx"
output_file = "output/MultipleTextWatermarks.pptx"

# 创建并加载PPT文档
presentation = Presentation()
presentation.LoadFromFile(input_file)

# 水印参数
watermark_width = 150.0
watermark_height = 100.0
grid_spacing_x = (presentation.SlideSize.Size.Width - watermark_width) / 4
grid_spacing_y = (presentation.SlideSize.Size.Height - watermark_height) / 4

# 遍历所有幻灯片
for slide in presentation.Slides:
    # 在3*3网格中添加水印
    for row in range(3):
        for col in range(3):
            # 计算水印位置
            left = col * grid_spacing_x + (col * watermark_width)
            top = row * grid_spacing_y + (row * watermark_height)
            rect = RectangleF(left, top, watermark_width, watermark_height)

            # 添加矩形形状作为水印
            shape = slide.Shapes.AppendShape(ShapeType.Rectangle, rect)

            # 设置矩形样式
            shape.Fill.FillType = FillFormatType.none
            shape.ShapeStyle.LineColor.Color = Color.get_White()
            shape.Rotation = -45
            shape.Locking.SelectionProtection = True
            shape.Line.FillType = FillFormatType.none

            # 添加文本到矩形
            shape.TextFrame.Text = "Watermark"
            text_range = shape.TextFrame.TextRange

            # 设置文本样式
            text_range.Fill.FillType = FillFormatType.Solid
            text_range.Fill.SolidColor.Color = Color.get_Blue()
            text_range.FontHeight = 20

# 保存并关闭文档
presentation.SaveToFile(output_file, FileFormat.Auto)
presentation.Dispose()

结果
Python添加重复文字水印到PowerPoint

用Python添加图片水印到演示文稿

我们还可以通过将指定图片设置幻灯片的背景,从而向PowerPoint演示文稿中插入图片水印。以下是操作步骤:

  1. 导入必要的类:Presentation, FileFormat, BackgroundType, FillFormatType, PictureFillType, Stream, RectangleAlignment
  2. 定义输入输出文件路径及图片路径:input_file, output_file, logo_path
  3. 创建并加载PPT文档:Presentation(), LoadFromFile(input_file)
  4. 加载图像到内存流,并将其添加到PPT中:Stream(logo_path), presentation.Images.AppendStream(stream)
  5. 设置幻灯片背景为自定义,并用图像填充作为水印:slide.SlideBackground.Type = BackgroundType.Custom, slide.SlideBackground.Fill.FillType = FillFormatType.Picture, slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch, slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image
  6. 保存并关闭文档:presentation.SaveToFile(output_file, FileFormat.Pptx2013), presentation.Dispose()

代码示例

from spire.presentation import Presentation, FileFormat, BackgroundType, FillFormatType, PictureFillType, Stream, \
    RectangleAlignment

# 定义输入输出文件路径
input_file = "Sample.pptx"
output_file = "output/AddImageWatermark.pptx"
logo_path = "Image.png"

# 创建并加载PPT文档
presentation = Presentation()
presentation.LoadFromFile(input_file)

# 加载图像并添加到PPT中
with Stream(logo_path) as stream:
    image = presentation.Images.AppendStream(stream)

# 设置幻灯片背景为自定义,并填充图像作为水印
slide = presentation.Slides[0]
slide.SlideBackground.Type = BackgroundType.Custom
slide.SlideBackground.Fill.FillType = FillFormatType.Picture
slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch
slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image

# 保存并关闭文档
presentation.SaveToFile(output_file, FileFormat.Pptx2013)
presentation.Dispose()

结果
Python插入图片水印到PowerPoint演示文稿

用Python添加重复图片水印到演示文稿

我们还可以通过将ISlide.SlideBackground.Fill.PictureFill.FillType属性设置为PictureFillType.Tile来实现在PowerPoint幻灯片中插入重复图片水印。注意,需要给水印图片足够的空白以免水印过于密集。以下是代码示例:

from spire.presentation import Presentation, FileFormat, BackgroundType, FillFormatType, PictureFillType, Stream

# 定义输入输出文件路径
input_file = "Sample.pptx"
output_file = "output/AddImageWatermark.pptx"
logo_path = "Watermark.png"

# 创建并加载PPT文档
from spire.presentation import Presentation, FileFormat, BackgroundType, FillFormatType, PictureFillType, Stream

# 定义输入输出文件路径
input_file = "G:/Documents/Sample27.pptx"
output_file = "output/MultipleImageWatermark.pptx"
logo_path = "G:/Documents/Watermark.png"

# 创建并加载PPT文档
presentation = Presentation()
presentation.LoadFromFile(input_file)

# 加载图像并添加到PPT中
with Stream(logo_path) as stream:
    image = presentation.Images.AppendStream(stream)

# 设置幻灯片背景为自定义,并填充图像作为水印
slide = presentation.Slides[0]
slide.SlideBackground.Type = BackgroundType.Custom
slide.SlideBackground.Fill.FillType = FillFormatType.Picture
# 将填充方式设置为堆叠从而设置重复图像水印
slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Tile
slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image

# 保存并关闭文档
presentation.SaveToFile(output_file, FileFormat.Auto)
presentation.Dispose()

结果
用Python添加重复图片水印到PowerPoint

本文演示了如何使用Python插入水印到PowerPoint演示文稿。


大丸子
12 声望3 粉丝