Add Document Password

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

  1. Instantiate the Merger class with the path to the source document.
  2. Create an AddPasswordOptions instance with the desired password string.
  3. Call merger.add_password() and pass the AddPasswordOptions object.
  4. Call merger.save() with the output file path to write the protected document.
from groupdocs.merger import Merger
from groupdocs.merger.domain.options import AddPasswordOptions

def add_document_password():
    # Load the source document
    with Merger("./input.pdf") as merger:
        # Define the password to apply
        options = AddPasswordOptions("p@ss")
        # Encrypt the document with the specified password
        merger.add_password(options)
        # Save the password-protected document
        merger.save("./output.pdf")

if __name__ == "__main__":
    add_document_password()

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

Binary file (PDF, 86 KB)

Download full output

Explanation

  • Load Source Document: The Merger class is instantiated with the path to the document that should be protected.
  • Configure Password: AddPasswordOptions("p@ss") sets the password that will be required to open the document.
  • Apply Protection: merger.add_password() encrypts the document using the supplied options.
  • Save Result: merger.save() writes the password-protected file to the specified output path.

Refer to the GroupDocs.Merger API Reference for full details on AddPasswordOptions and supported formats.

See also