Adding watermarks to Word documents is a common requirement in everyday office work. Whether you need to mark a document as “Internal Use,” “Confidential,” etc., to indicate its status, or add a company logo as a picture watermark to protect copyright, watermarks can effectively improve a document’s professionalism and security.
This article shows how to use a free Word library to add text and image watermarks to Word documents with concise Python code.
Preparation
In this scenario, we'll use Free Spire.Doc for Python package. You can install it with pip:
pip install spire.doc.free
After installation you can start writing code.
Add a Text Watermark to Word
Text watermarks are the most common type and are typically used to indicate document status. The code below shows how to add a text watermark to a Word document:
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load the Word document
document.LoadFromFile("Input.docx")
# Create a TextWatermark object
txtWatermark = TextWatermark()
# Set the text watermark's properties
txtWatermark.Text = "Internal Use Only" # Watermark text
txtWatermark.FontSize = 65 # Font size
txtWatermark.FontName = "Times New Roman" # Font name
txtWatermark.Color = Color.get_Green() # Font color
txtWatermark.Layout = WatermarkLayout.Diagonal # Layout (diagonal)
# Add the text watermark to the document
document.Watermark = txtWatermark
# Save the resulting document
document.SaveToFile("Output/TextWatermark.docx", FileFormat.Docx)
document.Close()
The core is the TextWatermark object; adjusting its properties lets you control the watermark’s appearance. The WatermarkLayout enum provides two layout options: Diagonal and Horizontal, so choose as needed.
Add an Image Watermark to Word
Image watermarks are commonly used to add a company logo, signature, or other graphic elements. Example code:
from spire.doc import *
from spire.doc.common import *
# Create a Document object
document = Document()
# Load the Word document
document.LoadFromFile("Input.docx")
# Create a PictureWatermark object
picture = PictureWatermark()
# Set the picture watermark's properties
picture.SetPicture("logo.png") # Path to the watermark image
picture.Scaling = 100 # Scaling percentage
picture.IsWashout = False # Whether to apply washout effect
# Add the picture watermark to the document
document.Watermark = picture
# Save the resulting document
document.SaveToFile("Output/ImageWatermark.docx", FileFormat.Docx)
document.Close()
The Scaling property controls the image size—the larger the value, the larger the image. The IsWashout property determines whether the washout (faded) effect is applied; setting it to False makes the image colors more vivid.
Complete Example and Notes
Combining the above code makes it easy to build a script that batch-adds watermarks. Keep these points in mind:
- File paths: Make sure input Word documents and image file paths are correct; create the output directory in advance.
- Document formats: The library supports formats such as .docx and .doc; specify the correct FileFormat when saving.
- Resource release: Call Close() after operations to release resources.
Summary
With Free Spire.Doc for Python, you can add watermarks to Word documents in just a few lines of code. The library provides TextWatermark and PictureWatermark classes for handling text and image watermarks respectively, and developers can configure style, position, transparency, and other properties as needed. Compared with manual Word operations, automating this with Python is more efficient and makes batch processing and integration into business systems easier.
Whether you’re protecting sensitive documents or standardizing corporate document style, mastering this technique will make your work easier. I hope this article is helpful!
Top comments (0)