How to Add Watermarks to PDF, Word, PPT, and Excel Using Python
This tutorial explains how to automate watermark insertion for PDF, Word, PowerPoint, and Excel documents with Python by installing appropriate libraries and providing ready-to-use code examples for each file type.
In daily work, adding watermarks to documents can be automated with Python. This article introduces the required libraries and provides complete code examples for inserting watermarks into PDF, Word, PowerPoint, and Excel files.
Adding a watermark to PDF
Install the PyPDF2 library: pip install PyPDF2 Code example:
import PyPDF2
pdfFile = open('example.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
pdfWriter = PyPDF2.PdfFileWriter()
for pageNum in range(pdfReader.getNumPages()):
pageObj = pdfReader.getPage(pageNum)
pageObj.mergePage(PyPDF2.PdfFileReader('watermark.pdf').getPage(0))
pdfWriter.addPage(pageObj)
resultPdfFile = open('watermarked.pdf', 'wb')
pdfWriter.write(resultPdfFile)
pdfFile.close()
resultPdfFile.close()This script adds the watermark from watermark.pdf to each page of example.pdf and saves the result as watermarked.pdf.
Adding a watermark to Word
Install the python-docx library: pip install python-docx Code example:
from docx import Document
from docx.shared import Inches
doc = Document()
section = doc.sections[0]
header = section.header
header.paragraphs[0].text = "水印"
doc.add_paragraph('这是一些正文内容。')
doc.save('watermarked.docx')The script creates a Word document, inserts a text watermark in the header, adds some body text, and saves it as watermarked.docx.
Adding a watermark to PowerPoint
Install the python-pptx library: pip install python-pptx Code example:
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Inches(1)
height = Inches(1.5)
width = Inches(6)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
tf.text = "水印"
prs.save('watermarked.pptx')This code creates a new slide, adds a textbox containing the watermark text, and saves the presentation as watermarked.pptx.
Adding a watermark to Excel
Install the openpyxl library: pip install openpyxl Code example:
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
wb = load_workbook('example.xlsx')
ws = wb.active
img = Image('watermark.png')
img.width = 200
img.height = 200
ws.add_image(img, 'C3')
wb.save('watermarked.xlsx')The script loads an existing workbook, inserts an image watermark at cell C3, and saves the modified file as watermarked.xlsx.
Conclusion
Python provides a variety of libraries that make it easy to programmatically add watermarks to different document formats, improving efficiency and reducing manual effort in everyday tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
