Load Password-Protected Document

GroupDocs.Merger for Python via .NET supports opening documents that are protected with an open password. To unlock the file, pass a LoadOptions instance — populated with the correct password — to the Merger constructor alongside the file path or stream.

Exception handling

If the document is protected and you do not supply a password, or if the supplied password is incorrect, the library raises one of the following exceptions from groupdocs.merger.exceptions:

  • PasswordRequiredException — raised when you try to open a protected document without providing any password.
  • IncorrectPasswordException — raised when the provided password does not match the document’s open password.

Steps to load a password-protected document

  1. Create a LoadOptions instance and set the password parameter to the document’s open password.
  2. Instantiate the Merger class with both the document path and the LoadOptions object.
  3. Perform any supported operation (merge, split, get info, etc.) on the open document.
from groupdocs.merger import Merger
from groupdocs.merger.domain.options import LoadOptions
from groupdocs.merger.exceptions import IncorrectPasswordException, PasswordRequiredException

def load_password_protected():
    # Provide the document's open password via LoadOptions
    load_options = LoadOptions(password="secret")
    try:
        # Load the password-protected document
        with Merger("./protected.pdf", load_options) as merger:
            # The document is now open — perform any supported operation
            info = merger.get_document_info()
            print("Format:", info.type.file_format)
            print("Pages:", info.page_count)
    except PasswordRequiredException:
        print("Error: the document requires a password but none was provided.")
    except IncorrectPasswordException:
        print("Error: the supplied password is incorrect.")

if __name__ == "__main__":
    load_password_protected()

protected.pdf is a sample file used in this example. Click here to download it.

Format: Portable Document Format File
Pages: 2

Download full output

Explanation

  • Load Options: LoadOptions(password="secret") wraps the open password. This options object is passed to the Merger constructor so the library can decrypt the document on load.
  • Open Document: Merger("./protected.pdf", load_options) opens the encrypted file. If the password is missing or wrong, the appropriate exception is raised before the with block body executes.
  • Perform Operations: Once open, you can call any Merger method — get_document_info(), join(), split(), save(), etc. — on the decrypted document.
  • Exception Handling: Wrapping the Merger construction in try/except blocks lets you give users a clear error message when the password is absent (PasswordRequiredException) or wrong (IncorrectPasswordException). Both are importable from groupdocs.merger.exceptions.

Refer to the GroupDocs.Merger API Reference for full details on LoadOptions and the exception hierarchy.

See also