As a Python developer, you can annotate any Word (.doc or .docx) file programmatically on the cloud. Annotations usually are metadata in the form of comments, notes, explanations, or other types of external remarks in the document providing additional information about an existing piece of data. This article will be focusing on how to annotate DOCX files using a REST API in Python.
The following topics shall be covered in this article:
- Document Annotation REST API and Python SDK
- Annotate DOCX Files using a REST API
- Add Multiple Annotations using Python
Document Annotation REST API and Python SDK
For annotating DOC or DOCX documents, I will be using the Python SDK of GroupDocs.Annotation Cloud API. It allows you to programmatically build online document and image annotation tools. Such tools can be used to add annotations, watermark overlays, text replacements, redactions, sticky notes, and text markups to the business documents of all popular formats. It also provides .NET, Java, PHP, Ruby, and Node.js SDKs as its document annotation family members for the Cloud API.
You can install GroupDocs.Annotation Cloud to your Python project using the following command in the console:
pip install groupdocs_annotation_cloud
Please get your Client ID and Client Secret from the dashboard before you start following the steps and available code examples. Add your ID and secret in the code as demonstrated below:
Annotate DOCX Files using a REST API in Python
You can add annotations to the DOCX file by following the simple steps mentioned below:
- Upload the DOCX file to the Cloud
- Add Annotations to DOCX Files in Python
- Download the updated file
Upload the Document
First of all, upload the DOCX file to the Cloud using the code example given below:
As a result, the uploaded DOCX file will be available in the files section of your dashboard on the cloud.
Add Annotations to DOCX Files in Python
Please follow the steps mentioned below to insert annotations in the Word document programmatically.
- Create an instance of AnnotationInfo
- Set various Annotation properties e.g. position, type, text, etc.
- Create a FileInfo instance
- Set the file path
- Create an instance of AnnotateOptions
- Set file info to AnnotateOptions
- Set annotation to AnnotateOptions
- Create a request by calling the AnnotateRequest method
- Get results by calling the AnnotateApi.annotate() method
The following code snippet shows how to insert area annotations in the Word document using a REST API.
As a result, the area annotations will be inserted in the document as shown below.
Supported Annotation Types
Please find below the list of supported annotation types, you can add to your DOCX files by following the steps mentioned earlier:
- Area
- Distance
- Link
- Point
- Polyline
- Image
- Text
- Watermark
- Arrow
Download the Updated File
The above code sample will save the annotated DOCX file on the cloud. You can download it using the following code sample:
Add Multiple Annotations using Python
Please follow the steps mentioned below to add multiple annotations to your DOCX files programmatically.
- Create first instance of AnnotationInfo
- Set various Annotation properties for the first instance e.g. position, type, text, etc.
- Create AnnotationInfo second instance
- Set various annotation properties for the second instance e.g. position, type, text, etc.
- Create a FileInfo instance
- Set the file path
- Create an instance of AnnotateOptions
- Set file info to AnnotateOptions
- Set first and second annotations to AnnotateOptions
- Create a request by calling the AnnotateRequest method
- Get results by calling the AnnotateApi.annotate() method
The following code snippet shows how to add multiple annotations to DOCX file using a REST API. Please follow the steps mentioned earlier to upload and download a file.
As a result, the image and watermark annotations will be inserted in the document as shown below.
Conclusion
In this article, you have learned how to add various types of annotations to Word documentson the cloud with Document Annotation REST API using Python. You also learned how to programmatically upload the DOCX file on the cloud and then download the annotated file from the cloud. You can learn more about GroupDocs.Annotation Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.