GroupDocs.Merger for Python via .NET lets you add an open password to any supported document format. Once protected, the document can only be opened by users who supply the correct password.
Steps to add a document password
Instantiate the Merger class with the path to the source document.
Create an AddPasswordOptions instance with the desired password string.
Call merger.add_password() and pass the AddPasswordOptions object.
Call merger.save() with the output file path to write the protected document.
fromgroupdocs.mergerimportMergerfromgroupdocs.merger.domain.optionsimportAddPasswordOptionsdefadd_document_password():# Load the source documentwithMerger("./input.pdf")asmerger:# Define the password to applyoptions=AddPasswordOptions("p@ss")# Encrypt the document with the specified passwordmerger.add_password(options)# Save the password-protected documentmerger.save("./output.pdf")if__name__=="__main__":add_document_password()
input.pdf is a sample file used in this example. Click here to download it.