How to Decrypt and Unlock a Password-Protected PDF File in Python
If your Python application is receiving encrypted and password-protected PDF files from trusted external sources, having a way to decrypt and unlock those files will be very important to your workflow.
Thankfully, the below PDF decryption API solution will allow you to complete this administrative task using only a few lines of ready-to-run Python code. User input is very simple; the only request parameters required are the PDF file path and a valid password supplied by the PDF creator.
To call this API, first run the following command to install the SDK:
pip install cloudmersive-convert-api-client
Next, add the imports and call the function:
from __future__ import print_function
import time
import cloudmersive_convert_api_client
from cloudmersive_convert_api_client.rest import ApiException
from pprint import pprint
# Configure API key authorization: Apikey
configuration = cloudmersive_convert_api_client.Configuration()
configuration.api_key['Apikey'] = 'YOUR_API_KEY'
# create an instance of the API class
api_instance = cloudmersive_convert_api_client.EditPdfApi(cloudmersive_convert_api_client.ApiClient(configuration))
password = 'password_example' # str | Valid password for the PDF file
input_file = '/path/to/inputfile' # file | Input file to perform the operation on.
try:
# Decrypt and password-protect a PDF
api_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)
Yep — that’s all the code you’ll need!
To complete your API call, include a free-tier Cloudmersive API key in the configuration.api_key field; you can get one by registering a free account on our website (free-tier API keys supply a limit of 800 API calls per month).