How to decrypt a PDF File and Disable its Password in Python

What do you say we skip the hot mess that is PDF decryption and hand out the results instead? It’s not as though the hand-written approach is going to have any extra benefits, besides maybe a nice headache. That said, let me show you how to use an API that’s capable of performing the decryption in our stead.

With pip install, our client can be installed as shown here:

pip install cloudmersive-convert-api-client

And below we have the layout for our function call for edit_pdf_decrypt.

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))password = 'password_example' # str | Valid password for the PDF fileinput_file = '/path/to/file' # file | Input file to perform the operation on.try:# Decrypt and password-protect a PDFapi_response = api_instance.edit_pdf_decrypt(password, input_file)pprint(api_response)except ApiException as e:print("Exception when calling EditPdfApi->edit_pdf_decrypt: %s\n" % e)

Now the API just needs the PDF’s password and the file itself. The decryption will be handled on the API’s end and you can just enjoy the results.

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