Add or Remove Watermarks in a Word Document in Python

Alexander Stock
3 min readOct 20, 2023

--

Add Watermarks to Word in Python

Watermarks serve as an effective way to enhance the appearance and protect the integrity of Word documents. A watermark is a faint image or text that appears in the background of a document, typically conveying information such as confidentiality notices, draft labels, or company logos. By incorporating watermarks, Word users can add a professional touch, reinforce branding, or indicate the status of a document. There may also be times when it is necessary to remove a watermark, for example, to change the ownership of a document or to make a document look clean.

In this article, I am going to introduce how to add or remove watermarks in a Word document in Python using Spire.Doc for Python library.

  • Add a Text Watermark to Word in Python
  • Add an Image Watermark to Word in Python
  • Remove Watermarks from a Word Document in Python

Install Dependency

This solution requires Spire.Doc for Python to be installed as a dependency, which is a Python library for reading, creating and manipulating Word documents in a Python program. You can install Spire.Doc for Python by executing the following pip command.

pip install Spire.Doc

Add a Text Watermark to Word in Python

The TextWatermark class provided by Spire.Doc for Python is used to create a text watermark and define the properties of it, such as the text, font size, font color and layout. The following code example shows you how to add a text watermark to an existing Word document.

from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()

# Load a Word document
document.LoadFromFile("Input.docx")

# Create a TextWatermark object
txtWatermark = TextWatermark()

# Set the format of the text watermark
txtWatermark.Text = "DO NOT COPY"
txtWatermark.FontSize = 65
txtWatermark.Color = Color.get_Red()
txtWatermark.Layout = WatermarkLayout.Diagonal

# Add the text watermark to document
document.Watermark = txtWatermark

# Save the result document
document.SaveToFile("Output/TextWatermark.docx", FileFormat.Docx)
document.Close()

Add an Image Watermark to Word in Python

Similarly, Spire.Doc for Python provides the PictureWatermark class to take care of setting picture watermarks for Word documents. The following code gives you an example.

from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()

# Load a Word document
document.LoadFromFile("Iuput.docx")

# Create a PictureWatermark object
picture = PictureWatermark()

# Set the format of the picture watermark
picture.SetPicture("logo.png")
picture.Scaling = 100
picture.IsWashout = False

# Add the image watermark to document
document.Watermark = picture

# Save the result document
document.SaveToFile("Output/ImageWatermark.docx", FileFormat.Docx)
document.Close()

Remove Watermarks from a Word Document in Python

No matter it’s an image watermark or a text watermark, it can be easily removed by setting the Document.Watermark property to None. The following code snippet gives you an example.

from spire.doc import *
from spire.doc.common import *

# Create an object of Document class
doc = Document()

# Load a Word document
doc.LoadFromFile("Watermark.docx")

# Remove the watermark
doc.Watermark = None

# Save the document
doc.SaveToFile("output/RemoveWatermark.docx", FileFormat.Docx)

--

--

Alexander Stock

I'm Alexander Stock, a software development consultant and blogger with 10+ years' experience. Specializing in office document tools and knowledge introduction.