How to Remove PDF Password with Python
Learn how to remove a known open password from a PDF using Python and the Encrypt PDF API Tool. This Remove PDF Password tutorial keeps the official sample intact and breaks down authentication, payload construction, endpoint behavior, and result handling for Python.
Why Remove PDF Password with Python?
Encrypted documents often need authorized preprocessing before they enter text extraction, archival, or analysis pipelines. Decrypt PDF accepts the existing open password and returns a new PDF resource without that encryption.
A Python ingestion process handling monthly financial reports can decrypt each approved input, continue with classification, and immediately remove temporary resources after saving the required result. That keeps credential handling inside one controlled stage.
Knowing the password is a prerequisite; the API does not crack or infer it. Callers should also distinguish removal of encryption from removal of other document restrictions, which is a separate operation and policy decision.
Python Code Example
from requests_toolbelt import MultipartEncoder
import requests
import json
# By default, we use the US-based API service. This is the primary endpoint for global use.
api_url = "https://api.pdfrest.com"
# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
#api_url = "https://eu-api.pdfrest.com"
# Toggle deletion of sensitive files (default: False)
DELETE_SENSITIVE_FILES = False
decrypted_pdf_endpoint_url = api_url+'/decrypted-pdf'
# The /decrypted-pdf endpoint can take a single PDF file or id as input.
# This sample demonstrates decryption of a PDF with the password 'password'.
mp_encoder_decryptedPdf = MultipartEncoder(
fields={
'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
'output' : 'example_decryptedPdf_out',
'current_open_password': 'password',
}
)
# Let's set the headers that the decrypted-pdf endpoint expects.
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
headers = {
'Accept': 'application/json',
'Content-Type': mp_encoder_decryptedPdf.content_type,
'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}
print("Sending POST request to decrypted-pdf endpoint...")
response = requests.post(decrypted_pdf_endpoint_url, data=mp_encoder_decryptedPdf, headers=headers)
print("Response status code: " + str(response.status_code))
if response.ok:
response_json = response.json()
print(json.dumps(response_json, indent = 2))
else:
print(response.text)
# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample.
# All files uploaded or generated are automatically deleted based on the
# File Retention Period as shown on https://pdfrest.com/pricing.
# For immediate deletion of files, particularly when sensitive data
# is involved, an explicit delete call can be made to the API.
#
# Deletes all files in the workflow, including outputs. Save all desired files before enabling this step.
if DELETE_SENSITIVE_FILES and response.ok:
result_id = response_json['outputId']
delete_data = { "ids": f"{response_json['inputId']}, {result_id}" }
delete_response = requests.post(url=api_url+'/delete',
data=json.dumps(delete_data),
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})
print("Delete response status code: " + str(delete_response.status_code))
print(json.dumps(delete_response.json(), indent = 2))
Source for Remove PDF Password: View the Python sample on GitHub.
Breaking Down the Code
Import the HTTP and multipart helpers
from requests_toolbelt import MultipartEncoder import requests import json
requests handles the HTTP exchange for Remove PDF Password, while MultipartEncoder constructs form-data bodies with a boundary that the request header can reuse. The remaining Python modules support the response or timing behavior used specifically by Remove PDF Password.
Select the pdfRest service region
# By default, we use the US-based API service. This is the primary endpoint for global use. api_url = "https://api.pdfrest.com" # For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below. # For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work #api_url = "https://eu-api.pdfrest.com" # Toggle deletion of sensitive files (default: False) DELETE_SENSITIVE_FILES = False
The Python configuration keeps the pdfRest hostname separate from each Remove PDF Password route. Using one regional base URL throughout this Python Remove PDF Password job keeps its returned identifiers available to later requests.
Encode the file and known password
mp_encoder_decryptedPdf = MultipartEncoder(
fields={
'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
'output' : 'example_decryptedPdf_out',
'current_open_password': 'password',
}
)
# Let's set the headers that the decrypted-pdf endpoint expects.
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
headers = {
'Accept': 'application/json',
'Content-Type': mp_encoder_decryptedPdf.content_type,
'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}
This block constructs the operation-specific input for Remove PDF Password. Its file parts carry binary content, while the named Remove PDF Password fields describe pdfRest behavior rather than syntax supplied by Python.
Send the authenticated multipart request
headers = {
'Accept': 'application/json',
'Content-Type': mp_encoder_decryptedPdf.content_type,
'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}
print("Sending POST request to decrypted-pdf endpoint...")
response = requests.post(decrypted_pdf_endpoint_url, data=mp_encoder_decryptedPdf, headers=headers)
print("Response status code: " + str(response.status_code))
if response.ok:
response_json = response.json()
print(json.dumps(response_json, indent = 2))
else:
print(response.text)
The Python request joins the Remove PDF Password endpoint, authentication header, and prepared body. For this Remove PDF Password call, the multipart metadata generated by Python must remain paired with the body so pdfRest can separate files from options.
Optionally delete both sensitive resources
if DELETE_SENSITIVE_FILES and response.ok:
result_id = response_json['outputId']
delete_data = { "ids": f"{response_json['inputId']}, {result_id}" }
delete_response = requests.post(url=api_url+'/delete',
data=json.dumps(delete_data),
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})
print("Delete response status code: " + str(delete_response.status_code))
print(json.dumps(delete_response.json(), indent = 2))
The optional Python cleanup path collects the sensitive input and output IDs and sends them to Delete Files. Enable this Remove PDF Password cleanup only after the Python application has preserved every result it needs.
Beyond the Tutorial
This walkthrough gives you an authorized Python path from a password-protected source to a decrypted PDF resource that downstream tools can use.
Source the password from protected runtime configuration, not the script or diagnostic output. Review the optional deletion block before enabling it because it removes both IDs and assumes the application has already preserved anything it needs.
Try the Remove PDF Password workflow with a representative file in API Lab, then adapt the request in Python. The Encrypt PDF API Tool documentation provides the complete Python context for Remove PDF Password values, defaults, and limitations.
Note: This Python Remove PDF Password tutorial uses a multipart file upload. For Remove PDF Password content already stored in pdfRest, the Python JSON payload example supplies a managed resource ID instead of uploading the file again.