Insert Watermark in Word Documents using Python

Watermark Word file

Documents are shared over the internet and they have the tendency to be counterfeited. Therefore, watermarking is one of the easiest techniques widely used to protect confidential information and to indicate the validity of a legal document. Usually, it’s an image or text that appears mainly behind the main document. In this article, we are going to discuss the details on how to insert watermark in Word documents using Python SDK.

Word Document Processing API

Aspose.Words Cloud is specifically created to offer the capabilities to create, edit and manipulate MS Word (DOCX, DOC, DOT, RTF, DOCM) or OpenDocument (ODT, OTT) files. The API is so robust that you can manipulate Word documents or even transform them to other supported formats with a couple of code lines. No software download is required and all the processing is performed within the cloud.

Now as per the scope of the current article, we are going to discuss further details pertinent to Aspose.Words Cloud SDK for Python which is a wrapper around Aspose.Words Cloud API. In order to use the API, the first step is the installation of SDK. It is available for download at PIP and GitHub. Execute the following command on the command line terminal to install the SDK:

 pip install aspose-words-cloud

If you are using Visual Studio for a Python project, click View -> Other Windows -> Python Environments option and enter aspose-word-cloud under Packages field in Python Environments window. Then click the Install aspose-word-cloud.

Python Environment
Image 1:- aspose-words-cloud python package.

Add Image Watermark in Word Document using Python

In this section, we are going to discuss the details and related steps on how to add text watermark in Word document using Python programming language.

  • Firstly, we need to create an instance of WordsApi class while passing Client ID and Client details as arguments
  • Secondly, use upload_file(..) method while passing the UploadFileRequest object as an argument to upload input Word document and image file to cloud storage (two seprate calls)
  • Now create an object of InsertWatermarkImageRequest class which takes input Word file name, resultant file name, rotating angel and the name of image to be watermarked
  • Finally, call the insert_watermark_image(..) method of WordsApi to insert text watermark in Word document
Image watermark preview
Image 2:- Preview of Word document with image watermark.

Insert Text Watermark in Word Document

Let’s discuss the details on how to add image watermark to Word documents using Python Cloud SDK.

  • Firstly, we need to create an instance of WordsApi class while passing Client ID and Client details as arguments
  • Secondly, use upload_file(..) method while passing the UploadFileRequest object as an argument to upload input Word documents to cloud storage
  • Thirdly, create an instance of WatermarkText class that takes rotating angle and text to be watermarked as arguments
  • Now create an object of InsertWatermarkTextRequest class which takes input Word file name, resultant file name and WatermarkText object as arguments
  • Finally, call the insert_watermark_text(..) method of WordsApi to insert text watermark in Word document
Text watermark preview
Image 3:- Preview of Text watermark inserted in Word document.

Add Text Watermark using cURL Command

The cURL commands can also be used to access Aspose.Words Cloud API. However, before proceeding further, we need to generate a JWT access token based on client credentials. Therefore, please execute the following command to geneate the token.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=88d1cda8-b12c-4a80-b1ad-c85ac483c5c5&client_secret=406b404b2df649611e508bbcfcd2a77f" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once we have the JWT Token, please execute the following command to insert text watermark in Word document and save the output with the name of TextWatermarked.doc in Cloud storage.

curl -v -X POST "https://api.aspose.cloud/v4.0/words/source.doc/watermarks/texts?destFileName=TextWatermarked.doc" \
-H  "accept: application/json" \
-H  "Authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{\"Text\":\"Confidential\",\"RotationAngle\":45}"

The sample files used in above examples can be downloaded from source.doc and TextWatermarked.doc.

Conclusion

In this article, we have discussed the details related to addition of Text and Image watermarks in MS Word documents. We have explored the options to either use the cURL command or watermark word document in Python. Our cloud SDKs are developed under MIT license and their complete code snippet is available for download over GitHub. You may consider exploring the enriched documentation of Cloud API to learn more about other features offered by the API. In case you encounter any issue while using the API or you have any related query, please feel free to contact via free product support forum.

Related Articles

We recommend you to visit the following articles to learn more about