Opening password-protected PDFs
Some PDFs are encrypted and stay closed until the correct password unlocks them. An automated pipeline has no one to prompt, so the password has to travel with the open request. Once Nutrient decrypts the document in memory, extraction, conversion, and export all work as they would on an unprotected file.
The Nutrient Python SDK supplies the password through the password setting on the document’s open settings. Set it before opening the file, and Nutrient uses it to unlock the document.
How Nutrient supports this workflow
The password lives on the open settings, and Nutrient applies it while opening the file — before Document.open returns — so the password has to be in place before the open call. Create a DocumentSettings instance, set the password on its open settings, and pass the instance to Document.open.
Nutrient resolves open settings from the document first and falls back to the SDK-wide settings — a default password set once on the SDK applies to every document, and a password set on a document’s settings overrides it. An unencrypted document ignores the password entirely, so passing one is always safe.
Complete implementation
This example opens a password-protected PDF, exports its content to Markdown, and handles a missing or incorrect password.
Import the required Nutrient classes:
from nutrient_sdk import Documentfrom nutrient_sdk import DocumentSettingsfrom nutrient_sdk import InvalidPasswordExceptionfrom nutrient_sdk import NutrientExceptionCreate a DocumentSettings instance and set the password on its open settings. Nutrient uses this password to decrypt the file as it opens:
def main(): settings = DocumentSettings() settings.open_settings.password = "test123"Open the encrypted PDF with the settings using a Python context manager(opens in a new tab), which closes the document automatically, and export the decrypted content to output.md. Catch InvalidPasswordException before the general NutrientException to handle password failures separately:
try: with Document.open("input_password_protected.pdf", settings) as document: document.export_as_markdown("output.md") print("Successfully opened the document and wrote output.md") except InvalidPasswordException as e: print(f"The password is missing or incorrect: {e}") raise SystemExit(1) except NutrientException as e: print(f"Error: {e}") raise SystemExit(1)
if __name__ == "__main__": main()The password unlocks the document in memory. Once it’s open, the document behaves like any other — the export reads its content as if the file were never encrypted.
Handle errors
A missing or incorrect password leaves the document encrypted, so Document.open raises an InvalidPasswordException — the example above catches it before the general NutrientException to report the problem or to retry with a different password. An unencrypted document ignores the password, so supplying one never triggers this exception.
Summary
Opening a password-protected PDF takes four steps:
- Create a
DocumentSettingsinstance. - Set the password on its open settings.
- Open the document with the settings, catching
InvalidPasswordExceptionfor password failures. - Export the decrypted content to a file.