---
title: "Opening password-protected PDFs | Nutrient Python SDK"
canonical_url: "https://www.nutrient.io/guides/python/extraction/open-password-protected-pdf/"
md_url: "https://www.nutrient.io/guides/python/extraction/open-password-protected-pdf.md"
last_updated: "2026-08-10T00:00:00.000Z"
description: "How to open password-protected PDF documents using Nutrient Python SDK."
---

# 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.

[Download sample](https://www.nutrient.io/downloads/samples/python/open-password-protected-pdf.zip)

## 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:

```python

from nutrient_sdk import Document
from nutrient_sdk import DocumentSettings
from nutrient_sdk import InvalidPasswordException
from nutrient_sdk import NutrientException

```

Create a `DocumentSettings` instance and set the password on its open settings. Nutrient uses this password to decrypt the file as it opens:

```python

def main():
    settings = DocumentSettings()
    settings.open_settings.password = "test123"

```

Open the encrypted PDF with the settings using a Python [context manager](https://docs.python.org/3/reference/datamodel.html#context-managers), which closes the document automatically, and export the decrypted content to `output.md`. Catch `InvalidPasswordException` before the general `NutrientException` to handle password failures separately:

```python

    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:

1. Create a `DocumentSettings` instance.

2. Set the password on its open settings.

3. Open the document with the settings, catching `InvalidPasswordException` for password failures.

4. Export the decrypted content to a file.
---

## Related pages

- [Nutrient Python SDK extraction guides](/guides/python/extraction.md)
- [Applying OCR to a PDF page](/guides/python/extraction/apply-ocr-to-pdf-page.md)
- [Applying OCR to a PDF document](/guides/python/extraction/apply-ocr-to-pdf.md)
- [Classifying documents](/guides/python/extraction/classify-document.md)
- [Generating image descriptions using Claude](/guides/python/extraction/describe-image-with-claude.md)
- [Generating image descriptions using local AI](/guides/python/extraction/describe-image-with-local-ai.md)
- [Generating image descriptions using OpenAI](/guides/python/extraction/describe-image-with-openai.md)
- [Detecting document language](/guides/python/extraction/detect-document-language.md)
- [Extracting data from images using ICR](/guides/python/extraction/extract-data-from-image-icr.md)
- [Extracting data from images using OCR](/guides/python/extraction/extract-data-from-image-ocr.md)
- [Extracting data from images using vision language models](/guides/python/extraction/extract-data-from-image-vlm.md)
- [Extracting data from specific PDF pages](/guides/python/extraction/extract-data-from-specific-pages.md)
- [Extracting form fields from images](/guides/python/extraction/extract-form-fields-from-image.md)
- [Extracting structured data from documents](/guides/python/extraction/extract-structured-data.md)
- [Generating extraction schemas](/guides/python/extraction/generate-extraction-schema.md)
- [Extracting structured JSON data from PDF documents](/guides/python/extraction/json-data-extraction.md)
- [Labeling form fields with a vision language model](/guides/python/extraction/label-form-fields-with-vlm.md)
- [Extracting text from PDF documents](/guides/python/extraction/pdf-to-text.md)
- [Reading barcodes with vision extraction](/guides/python/extraction/read-barcodes-with-vision.md)
- [Extracting text from multilingual images](/guides/python/extraction/read-text-from-image-multi-language.md)
- [Extracting text from images](/guides/python/extraction/read-text-from-image.md)
- [Later, in another process — no document needed:](/guides/python/extraction/search-document-text.md)
- [Speeding up first ICR operation by predownloading models](/guides/python/extraction/speed-up-first-icr-by-downloading-requirements.md)
- [Split documents](/guides/python/extraction/split-document.md)

