SoFunction
Updated on 2024-10-28

Python manipulate word to achieve the watermark to add text or images

In Word documents, you can add a translucent graphic or text as a watermark to protect the originality of the document from unauthorized copying or use. In addition to providing a security feature, watermarks can display information about the document creator, additional document information, or simply be used for document decoration. This article describes how to use for PythonEase in the programAdd text and image watermarks to Word documents

Introducing for Python

Before using this tool to manipulate Word documents, you need to introduce this Python Word library into your project. This can be done from the official websiteDownload Productsor install it directly using the following pip command.

pip install 

Inserting a text watermark in a Word document

for Python providesTextWatermark class to set the text watermark. After setting up the property to add it to the Word document. The following is an example of how to do this:

  • Creates a Document object.
  • Use the () method to load a sample Word document.
  • Creates an instance of the TextWatermark class.
  • Use the methods of the TextWatermark class to set the text, font size, color and layout of the text watermark.
  • Use the properties to add a text watermark to a Word document.
  • Use the () method to save the results document.
from  import *
from  import *

# Create a Document object
document = Document()

# Load a Word document
("Example.docx")

# Create a TextWatermark object
txtWatermark = TextWatermark()

# Set the format of the text watermark
 = "Do not copy."
 = 65
 = Color.get_Red()
 = 

# Add a text watermark to a document
 = txtWatermark

# Save the results document
("output/text-watermark.docx", )
()

Adding Picture Watermarks to Word Documents

Image watermarks can be usedPictureWatermark class settings. The following is an example of the operation:

  • Creates a Document object.
  • Use the () method to load a sample Word document.
  • Creates an instance of the PictureWatermark class.
  • Use the () method to load an image as an image watermark, and then set the scaling and washout properties of the image watermark.
  • Use the properties to add an image watermark to a Word document.
  • Use the () method to save the results document.
from  import *
from  import *

# Create a Document object
document = Document()

# Load a Word document
("Example.docx")

# Create a PictureWatermark object
picture = PictureWatermark()

# Set the format of the image watermark
("")
 = 100
 = False

# Add image watermarks to documents
 = picture

# Save the results document
("output/image-watermark.docx", )
()

Anyone interested in learning more about how this third-party Python library operates on Word documents can go to the for Python TutorialView.

Above is Python operation word to achieve add text or image watermark details, more information about python word to add a watermark, please pay attention to my other related articles!