Remove Document Password

GroupDocs.Merger for Python via .NET lets you remove an open password from a protected document. After the operation the saved document is unencrypted and can be opened without any password.

Steps to remove a document password

  1. Create a LoadOptions instance and supply the current password so the document can be opened.
  2. Instantiate the Merger class with the protected document path and the LoadOptions object.
  3. Call merger.remove_password() to strip the password.
  4. Call merger.save() with the output file path to write the decrypted document.
from groupdocs.merger import Merger
from groupdocs.merger.domain.options import LoadOptions

def remove_document_password():
    # Supply the current password so the protected document can be opened
    load_options = LoadOptions(password="p@ss")
    # Load the password-protected document
    with Merger("./protected.pdf", load_options) as merger:
        # Remove the open password from the document
        merger.remove_password()
        # Save the decrypted (unprotected) document
        merger.save("./output.pdf")

if __name__ == "__main__":
    remove_document_password()

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

Binary file (PDF, 85 KB)

Download full output

Explanation

  • Load Options: LoadOptions(password="p@ss") provides the current document password so that Merger can decrypt and open the file. Omitting the password when the document is protected raises IncorrectPasswordException.
  • Load Document: The Merger context manager opens the protected file using the supplied credentials.
  • Remove Password: merger.remove_password() clears the open password from the in-memory document representation.
  • Save Result: merger.save() writes the now-unprotected document to disk.

Refer to the GroupDocs.Merger API Reference for full details on remove_password() and LoadOptions.

See also