How to remove a specific PDF annotation or comment in Python

Alright Python users, buckle your seat belts, because this one’s going to be fast!

Let’s install our client first:

pip install cloudmersive-convert-api-client

And now we just need to write up the function call for edit_pdf_remove_annotation_item. Don’t forget to use an API key for instantiating the API. See below:

from __future__ import print_functionimport timeimport cloudmersive_convert_api_clientfrom cloudmersive_convert_api_client.rest import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_convert_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_convert_api_client.EditPdfApi(cloudmersive_convert_api_client.ApiClient(configuration))input_file = '/path/to/file' # file | Input file to perform the operation on.annotation_index = 56 # int | The 0-based index of the annotation in the documenttry:# Remove a specific PDF annotation, comment in the documentapi_response = api_instance.edit_pdf_remove_annotation_item(input_file, annotation_index)pprint(api_response)except ApiException as e:print("Exception when calling EditPdfApi->edit_pdf_remove_annotation_item: %s\n" % e)

Now we toss in our file path and the index number for our annotation. Run it, and your file will be returned, sans-annotation! If you would instead like to remove all of the annotations in the PDF file, there is a separate method for that in the same library.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store