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

相关文章

  • 手把手教你部署AI小说生成器(Python环境搭建)

    手把手教你部署AI小说生成器(Python环境搭建)

    本文详细介绍了如何使用Python+AI从零开始搭建属于自己的AI小说生成器,文章分为环境准备、第一次安装、关机重启、配置大脑等步骤进行讲解,帮助用户轻松实现AI辅助创作
    2026-04-04
  • Python+wxPython打造一个网页图片一键下载神器(附完整源码)

    Python+wxPython打造一个网页图片一键下载神器(附完整源码)

    这篇文章主要为大家详细介绍了如何使用Python和wxPython打造一个网页图片一键下载神器,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下
    2025-11-11
  • Python参数传递及收集机制原理解析

    Python参数传递及收集机制原理解析

    这篇文章主要介绍了Python参数传递及收集机制原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • python中upper是做什么用的

    python中upper是做什么用的

    在本篇文章里小编给大家整理的是一篇关于python中upper的作用的相关文章,有需要的朋友们可以参考下。
    2020-07-07
  • Python webargs 模块的简单使用

    Python webargs 模块的简单使用

    webargs是一个用于解析和验证HTTP请求对象的Python库,今天通过本文给大家介绍Python webargs 模块的安装使用,感兴趣的朋友一起看看吧
    2022-01-01
  • python中利用matplotlib读取灰度图的例子

    python中利用matplotlib读取灰度图的例子

    今天小编就为大家分享一篇python中利用matplotlib读取灰度图的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • Selenium显式等待配置错误的报错与修复实战指南

    Selenium显式等待配置错误的报错与修复实战指南

    在自动化测试中,等待机制是处理页面元素加载延迟的重要手段,显式等待允许我们在继续执行代码之前等待某个条件发生,这比固定的强制等待更灵活高效,我们经常会遇到Selenium显式等待配置错误,所以本文给大家介绍了修复指南,需要的朋友可以参考下
    2025-07-07
  • Python导出DBF文件到Excel的方法

    Python导出DBF文件到Excel的方法

    这篇文章主要介绍了Python导出DBF文件到Excel的方法,实例分析了Python基于win32com模块实现文件导出与转换的相关技巧,需要的朋友可以参考下
    2015-07-07
  • opencv-python的RGB与BGR互转方式

    opencv-python的RGB与BGR互转方式

    这篇文章主要介绍了opencv-python的RGB与BGR互转方式,具有很好的参考价值,希望对大家有所 帮助。一起跟随小编过来看看吧
    2020-06-06
  • python scp 批量同步文件的实现方法

    python scp 批量同步文件的实现方法

    今天小编就为大家分享一篇python scp 批量同步文件的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01

最新评论