python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python PowerPoint添加水印

使用Python在PowerPoint幻灯片中添加水印

作者:Eiceblue

在PowerPoint演示文稿中插入文本水印是保护版权并确保文档内容真实性的有效方式,本文将演示如何使用Python插入文字水印到PowerPoint演示文稿,感兴趣的可以了解下

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

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

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

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

代码示例

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幻灯片中,以指定间隔重复添加水印文字来实现多行文字水印的插入。以下是代码示例:

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演示文稿中插入图片水印。以下是操作步骤:

代码示例

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添加重复图片水印到演示文稿

我们还可以通过将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添加水印内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文