欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2024年11月12日 08:19:14   作者:Eiceblue  
在PowerPoint演示文稿中插入文本水印是保護版權(quán)并確保文檔內(nèi)容真實性的有效方式,本文將演示如何使用Python插入文字水印到PowerPoint演示文稿,感興趣的可以了解下

在PowerPoint演示文稿中插入文本水印是保護版權(quán)并確保文檔內(nèi)容真實性的有效方式。利用Python,開發(fā)者通過簡單的代碼添加水印到PowerPoint幻燈片中,進行批量處理,允許精確控制水印的位置和外觀,便于集成到更大的應(yīng)用程序中。本文將演示如何使用Python插入文字水印到PowerPoint演示文稿。

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

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

我們可以通過在幻燈片中添加帶有選中保護的透明文本框,并在其中插入水印文字,來實現(xiàn)在PowerPoint演示文稿文字水印的添加。以下是操作步驟:

  • 導入必要的類:Presentation, FileFormat, RectangleF, ShapeType, FillFormatType, Color。
  • 定義輸入輸出文件路徑:input_file, output_file。
  • 加載PPT文檔:Presentation(), LoadFromFile()。
  • 計算水印位置:SlideSize.Size.Width, SlideSize.Size.Height。
  • 添加矩形水印:Shapes.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)

# 計算水印位置
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ù)添加水印文字來實現(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):
            # 計算水印位置
            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演示文稿中插入圖片水印。以下是操作步驟:

  • 導入必要的類: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來實現(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)文章

  • Python中全局變量和局部變量的理解與區(qū)別

    Python中全局變量和局部變量的理解與區(qū)別

    這篇文章主要給大家介紹了關(guān)于Python中全局變量和局部變量的理解與區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • Python讀取TIF影像的多種方法

    Python讀取TIF影像的多種方法

    Python提供了豐富的庫來讀取和處理TIFF文件,其中PIL庫是最常用的,本文給大家介紹Python讀取TIF影像的幾種方法,需要的朋友可以參考下
    2023-07-07
  • Python開發(fā)桌面小程序功能

    Python開發(fā)桌面小程序功能

    這篇文章主要介紹了Python開發(fā)一個桌面小程序功能,開發(fā)環(huán)境界面設(shè)置,功能介紹結(jié)合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • 如何用python免費看美劇

    如何用python免費看美劇

    在本篇文章里小編給大家整理的是關(guān)于如何用python免費看美劇的方法內(nèi)容,需要的朋友們可以學習下。
    2020-08-08
  • 一篇文章帶你深入學習Python函數(shù)

    一篇文章帶你深入學習Python函數(shù)

    這篇文章主要帶大家深入學習Python函數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • Python機器學習pytorch模型選擇及欠擬合和過擬合詳解

    Python機器學習pytorch模型選擇及欠擬合和過擬合詳解

    如何發(fā)現(xiàn)可以泛化的模式是機器學習的根本問題,將模型在訓練數(shù)據(jù)上過擬合得比潛在分布中更接近的現(xiàn)象稱為過擬合,用于對抗過擬合的技術(shù)稱為正則化
    2021-10-10
  • django models里數(shù)據(jù)表插入數(shù)據(jù)id自增操作

    django models里數(shù)據(jù)表插入數(shù)據(jù)id自增操作

    這篇文章主要介紹了django models里數(shù)據(jù)表插入數(shù)據(jù)id自增操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 關(guān)于python多重賦值的小問題

    關(guān)于python多重賦值的小問題

    這篇文章主要給大家介紹了關(guān)于python多重賦值的小問題,文中通過示例代碼介紹的非常詳細,對大家學習或者使用python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-04-04
  • 清理pip和conda緩存的方法示例

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

    本文主要介紹了清理pip和conda緩存的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-03-03
  • python圖片合成的示例

    python圖片合成的示例

    這篇文章主要介紹了python圖片合成的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-11-11

最新評論