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

 更新时间:2024年11月12日 08:19:14   作者:Eiceblue  
在PowerPoint演示文稿中插入文本水印是保护版权并确保文档内容真实性的有效方式,本文将演示如何使用Python插入文字水印到PowerPoint演示文稿,感兴趣的可以了解下

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

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

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

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

  • 导入必要的类:Presentation, FileFormat, RectangleF, ShapeType, FillFormatType, Color。
  • 定义输入输出文件路径:input_file, output_file。
  • 加载PPT文档:Presentation(), LoadFromFile()。
  • 计算水印位置:SlideSize.Size.Width, SlideSize.Size.Height。
  • 添加矩形水印:Shapes.AppendShape(), RectangleF()。
  • 设置矩形样式:FillType, LineColor.Color, Rotation, SelectionProtection, Line.FillType。
  • 添加文本至矩形:TextFrame.Text。
  • 设置文本样式:FillType, SolidColor.Color, FontHeight。
  • 保存与关闭文档: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幻灯片中,以指定间隔重复添加水印文字来实现多行文字水印的插入。以下是代码示例:

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

  • 导入必要的类:Presentation, FileFormat, BackgroundType, FillFormatType, PictureFillType, Stream, RectangleAlignment。
  • 定义输入输出文件路径及图片路径:input_file, output_file, logo_path。
  • 创建并加载PPT文档:Presentation(), LoadFromFile(input_file)。
  • 加载图像到内存流,并将其添加到PPT中:Stream(logo_path), presentation.Images.AppendStream(stream)。
  • 设置幻灯片背景为自定义,并用图像填充作为水印: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()。

代码示例

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

相关文章

  • Python中全局变量和局部变量的理解与区别

    Python中全局变量和局部变量的理解与区别

    这篇文章主要给大家介绍了关于Python中全局变量和局部变量的理解与区别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Python读取TIF影像的多种方法

    Python读取TIF影像的多种方法

    Python提供了丰富的库来读取和处理TIFF文件,其中PIL库是最常用的,本文给大家介绍Python读取TIF影像的几种方法,需要的朋友可以参考下
    2023-07-07
  • Python开发桌面小程序功能

    Python开发桌面小程序功能

    这篇文章主要介绍了Python开发一个桌面小程序功能,开发环境界面设置,功能介绍结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • 如何用python免费看美剧

    如何用python免费看美剧

    在本篇文章里小编给大家整理的是关于如何用python免费看美剧的方法内容,需要的朋友们可以学习下。
    2020-08-08
  • 一篇文章带你深入学习Python函数

    一篇文章带你深入学习Python函数

    这篇文章主要带大家深入学习Python函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • Python机器学习pytorch模型选择及欠拟合和过拟合详解

    Python机器学习pytorch模型选择及欠拟合和过拟合详解

    如何发现可以泛化的模式是机器学习的根本问题,将模型在训练数据上过拟合得比潜在分布中更接近的现象称为过拟合,用于对抗过拟合的技术称为正则化
    2021-10-10
  • django models里数据表插入数据id自增操作

    django models里数据表插入数据id自增操作

    这篇文章主要介绍了django models里数据表插入数据id自增操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • 关于python多重赋值的小问题

    关于python多重赋值的小问题

    这篇文章主要给大家介绍了关于python多重赋值的小问题,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • 清理pip和conda缓存的方法示例

    清理pip和conda缓存的方法示例

    本文主要介绍了清理pip和conda缓存的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-03-03
  • python图片合成的示例

    python图片合成的示例

    这篇文章主要介绍了python图片合成的示例,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-11-11

最新评论