GroupDocs.Merger for Python via .NET lets you check whether a document has an open password set. The is_password_set() method on the Merger instance returns True if the document requires a password to open, or False otherwise.
Steps to check document password protection
Open the document with Merger. If the document is already password-protected, supply the password via LoadOptions; otherwise omit it.
Call merger.is_password_set() and inspect the return value.
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domain.optionsimportLoadOptionsdefcheck_document_password_protection():# Provide the current password so the protected document can be openedload_options=LoadOptions(password="p@ss")# Load the password-protected documentwithMerger("./protected.pdf",load_options)asmerger:# Returns True if the document has an open password setprotected=merger.is_password_set()print("Is password set:",protected)if__name__=="__main__":check_document_password_protection()
protected.pdf is a sample file used in this example. Click here to download it.
is_password_set() is a method on the Merger instance, not a property of the document information object returned by get_document_info().
Explanation
Supply Load Password: LoadOptions(password="p@ss") allows Merger to open the encrypted document. Without this, opening a protected document raises IncorrectPasswordException.
Load Document: The Merger context manager opens the document using the provided credentials.
Check Protection: merger.is_password_set() interrogates the loaded document and returns True if an open password is present, False if the document is unprotected.