A watermark is a semitransparent text or an image placed behind the content of a document. In Word, you can add a watermark to protect the intellectual property of the document, for example to include a copyright symbol, author's name or company logo. Or you can use it to indicate the status of a document, such as "Draft", "Confidential", or "Final". This article will demonstrate how to add text watermarks and image watermarks to Word in Python using Spire.Doc for Python.
Install Spire.Doc for Python
This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.Doc
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows
Add a Text Watermark to a Word Document in Python
Spire.Doc for Python provides the TextWatermark class to set a text watermark, and then you can add it to Word document through Document.Watermark property. The following are the detailed steps.
- Create a Document object.
- Load a sample Word document using Document.LoadFromFile() method.
- Create an instance of TextWatermark class.
- Set the text, font size, color and layout of the text watermark using the methods of TextWatermark class.
- Add the text watermark to the Word document using Document.Watermark property.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document object document = Document() # Load a Word document document.LoadFromFile("test.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 in a Word Document in Python
To set the image watermark, you can use the methods of PictureWatermark class. The following are the detailed steps.
- Create a Document object.
- Load a sample Word document using Document.LoadFromFile() method.
- Create an instance of PictureWatermark class.
- Load an image as the image watermark using PictureWatermark.SetPicture() method, and then set scaling as well as washout property of the image watermark.
- Add the image watermark to the Word document using Document.Watermark property.
- Save the result document using Document.SaveToFile() method.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document object document = Document() # Load a Word document document.LoadFromFile("test.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()
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.