GroupDocs.Merger for Python via .NET lets you replace an existing open password on a protected document with a new one. The saved document will require the new password to open.
Steps to update a document password
Create a LoadOptions instance and supply the current password so the document can be opened.
Instantiate the Merger class with the protected document path and the LoadOptions object.
Create an UpdatePasswordOptions instance with the new password string.
Call merger.update_password() and pass the UpdatePasswordOptions object.
Call merger.save() with the output file path to write the re-encrypted document.
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domain.optionsimportLoadOptions,UpdatePasswordOptionsdefupdate_document_password():# Supply the current (old) password so the protected document can be openedload_options=LoadOptions(password="old_p@ss")# Load the password-protected documentwithMerger("./protected.pdf",load_options)asmerger:# Define the replacement passwordupdate_options=UpdatePasswordOptions("new_p@ss")# Apply the new password to the documentmerger.update_password(update_options)# Save the document encrypted with the new passwordmerger.save("./output.pdf")if__name__=="__main__":update_document_password()
protected.pdf is a sample file used in this example. Click here to download it.
Load Options: LoadOptions(password="old_p@ss") provides the current document password so that Merger can decrypt and open the file. If the wrong password is provided, IncorrectPasswordException is raised.
Load Document: The Merger context manager opens the protected file using the current credentials.
Configure New Password: UpdatePasswordOptions("new_p@ss") wraps the replacement password.
Apply Update: merger.update_password() replaces the old password with the new one in the in-memory document.
Save Result: merger.save() writes the document encrypted with the new password to disk.