使用Python在PowerPoint幻燈片中添加水印
在PowerPoint演示文稿中插入文本水印是保護(hù)版權(quán)并確保文檔內(nèi)容真實(shí)性的有效方式。利用Python,開發(fā)者通過簡單的代碼添加水印到PowerPoint幻燈片中,進(jìn)行批量處理,允許精確控制水印的位置和外觀,便于集成到更大的應(yīng)用程序中。本文將演示如何使用Python插入文字水印到PowerPoint演示文稿。
本文所使用的方法需要用到Spire.Presentation for Python,PyPI:pip install spire.presentation。
用Python添加文字水印到演示文稿
我們可以通過在幻燈片中添加帶有選中保護(hù)的透明文本框,并在其中插入水印文字,來實(shí)現(xiàn)在PowerPoint演示文稿文字水印的添加。以下是操作步驟:
- 導(dǎo)入必要的類:Presentation, FileFormat, RectangleF, ShapeType, FillFormatType, Color。
- 定義輸入輸出文件路徑:input_file, output_file。
- 加載PPT文檔:Presentation(), LoadFromFile()。
- 計(jì)算水印位置:SlideSize.Size.Width, SlideSize.Size.Height。
- 添加矩形水?。篠hapes.AppendShape(), RectangleF()。
- 設(shè)置矩形樣式:FillType, LineColor.Color, Rotation, SelectionProtection, Line.FillType。
- 添加文本至矩形:TextFrame.Text。
- 設(shè)置文本樣式:FillType, SolidColor.Color, FontHeight。
- 保存與關(guān)閉文檔:SaveToFile(), Dispose()。
代碼示例
from spire.presentation import Presentation, FileFormat, RectangleF, ShapeType, FillFormatType, Color # 定義輸入輸出文件路徑 input_file = "Sample.pptx" output_file = "output/SingleTextWatermark.pptx" # 創(chuàng)建并加載PPT文檔 presentation = Presentation() presentation.LoadFromFile(input_file) # 計(jì)算水印位置 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) # 設(shè)置矩形樣式 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 # 設(shè)置文本樣式 text_range.Fill.FillType = FillFormatType.Solid text_range.Fill.SolidColor.Color = Color.get_Blue() text_range.FontHeight = 50 # 保存并關(guān)閉文檔 presentation.SaveToFile(output_file, FileFormat.Pptx2010) presentation.Dispose()
結(jié)果

用Python插入重復(fù)文字水印到演示文稿
除了插入單一文字水印外,我們還可以通過在PowerPoint幻燈片中,以指定間隔重復(fù)添加水印文字來實(shí)現(xiàn)多行文字水印的插入。以下是代碼示例:
from spire.presentation import Presentation, FileFormat, RectangleF, ShapeType, FillFormatType, Color
# 定義輸入輸出文件路徑
input_file = "Sample.pptx"
output_file = "output/MultipleTextWatermarks.pptx"
# 創(chuàng)建并加載PPT文檔
presentation = Presentation()
presentation.LoadFromFile(input_file)
# 水印參數(shù)
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網(wǎng)格中添加水印
for row in range(3):
for col in range(3):
# 計(jì)算水印位置
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)
# 設(shè)置矩形樣式
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
# 設(shè)置文本樣式
text_range.Fill.FillType = FillFormatType.Solid
text_range.Fill.SolidColor.Color = Color.get_Blue()
text_range.FontHeight = 20
# 保存并關(guān)閉文檔
presentation.SaveToFile(output_file, FileFormat.Auto)
presentation.Dispose()
結(jié)果

用Python添加圖片水印到演示文稿
我們還可以通過將指定圖片設(shè)置幻燈片的背景,從而向PowerPoint演示文稿中插入圖片水印。以下是操作步驟:
- 導(dǎo)入必要的類:Presentation, FileFormat, BackgroundType, FillFormatType, PictureFillType, Stream, RectangleAlignment。
- 定義輸入輸出文件路徑及圖片路徑:input_file, output_file, logo_path。
- 創(chuàng)建并加載PPT文檔:Presentation(), LoadFromFile(input_file)。
- 加載圖像到內(nèi)存流,并將其添加到PPT中:Stream(logo_path), presentation.Images.AppendStream(stream)。
- 設(shè)置幻燈片背景為自定義,并用圖像填充作為水?。簊lide.SlideBackground.Type = BackgroundType.Custom, slide.SlideBackground.Fill.FillType = FillFormatType.Picture, slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch, slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image。
- 保存并關(guān)閉文檔: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"
# 創(chuàng)建并加載PPT文檔
presentation = Presentation()
presentation.LoadFromFile(input_file)
# 加載圖像并添加到PPT中
with Stream(logo_path) as stream:
image = presentation.Images.AppendStream(stream)
# 設(shè)置幻燈片背景為自定義,并填充圖像作為水印
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
# 保存并關(guān)閉文檔
presentation.SaveToFile(output_file, FileFormat.Pptx2013)
presentation.Dispose()
結(jié)果

用Python添加重復(fù)圖片水印到演示文稿
我們還可以通過將ISlide.SlideBackground.Fill.PictureFill.FillType屬性設(shè)置為PictureFillType.Tile來實(shí)現(xiàn)在PowerPoint幻燈片中插入重復(fù)圖片水印。注意,需要給水印圖片足夠的空白以免水印過于密集。以下是代碼示例:
from spire.presentation import Presentation, FileFormat, BackgroundType, FillFormatType, PictureFillType, Stream
# 定義輸入輸出文件路徑
input_file = "Sample.pptx"
output_file = "output/AddImageWatermark.pptx"
logo_path = "Watermark.png"
# 創(chuàng)建并加載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"
# 創(chuàng)建并加載PPT文檔
presentation = Presentation()
presentation.LoadFromFile(input_file)
# 加載圖像并添加到PPT中
with Stream(logo_path) as stream:
image = presentation.Images.AppendStream(stream)
# 設(shè)置幻燈片背景為自定義,并填充圖像作為水印
slide = presentation.Slides[0]
slide.SlideBackground.Type = BackgroundType.Custom
slide.SlideBackground.Fill.FillType = FillFormatType.Picture
# 將填充方式設(shè)置為堆疊從而設(shè)置重復(fù)圖像水印
slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Tile
slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image
# 保存并關(guān)閉文檔
presentation.SaveToFile(output_file, FileFormat.Auto)
presentation.Dispose()
結(jié)果

到此這篇關(guān)于使用Python在PowerPoint幻燈片中添加水印的文章就介紹到這了,更多相關(guān)Python PowerPoint添加水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
手把手教你部署AI小說生成器(Python環(huán)境搭建)
本文詳細(xì)介紹了如何使用Python+AI從零開始搭建屬于自己的AI小說生成器,文章分為環(huán)境準(zhǔn)備、第一次安裝、關(guān)機(jī)重啟、配置大腦等步驟進(jìn)行講解,幫助用戶輕松實(shí)現(xiàn)AI輔助創(chuàng)作2026-04-04
Python+wxPython打造一個(gè)網(wǎng)頁圖片一鍵下載神器(附完整源碼)
這篇文章主要為大家詳細(xì)介紹了如何使用Python和wxPython打造一個(gè)網(wǎng)頁圖片一鍵下載神器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2025-11-11
Selenium顯式等待配置錯(cuò)誤的報(bào)錯(cuò)與修復(fù)實(shí)戰(zhàn)指南
在自動化測試中,等待機(jī)制是處理頁面元素加載延遲的重要手段,顯式等待允許我們在繼續(xù)執(zhí)行代碼之前等待某個(gè)條件發(fā)生,這比固定的強(qiáng)制等待更靈活高效,我們經(jīng)常會遇到Selenium顯式等待配置錯(cuò)誤,所以本文給大家介紹了修復(fù)指南,需要的朋友可以參考下2025-07-07
opencv-python的RGB與BGR互轉(zhuǎn)方式
這篇文章主要介紹了opencv-python的RGB與BGR互轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對大家有所 幫助。一起跟隨小編過來看看吧2020-06-06
python scp 批量同步文件的實(shí)現(xiàn)方法
今天小編就為大家分享一篇python scp 批量同步文件的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01

