使用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機器學習pytorch模型選擇及欠擬合和過擬合詳解
如何發(fā)現(xiàn)可以泛化的模式是機器學習的根本問題,將模型在訓練數(shù)據(jù)上過擬合得比潛在分布中更接近的現(xiàn)象稱為過擬合,用于對抗過擬合的技術(shù)稱為正則化2021-10-10django models里數(shù)據(jù)表插入數(shù)據(jù)id自增操作
這篇文章主要介紹了django models里數(shù)據(jù)表插入數(shù)據(jù)id自增操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07