This short tutorial guides on how to change password of PDF using Python. It provides a set of steps to be performed to achieve this requirement along with the runnable sample code demonstrating how to change PDF password using Python. You will get an option to set both the owner and user password again while using the existing password.
Steps to Change Password of PDF using Python
- Establish the environment to use Aspose.PDF for Python via .NET to change password
- Create the PdfFileInfo class object to check the encryption state of the target PDF
- Check if the target PDF file is encrypted, perform the steps below
- Create an object of the PdfFileSecurity class
- Call the bind_pdf() method to bind the target PDF file
- Call the change_password() method using the current owner password, new user password, and owner password
- Save the resultant PDF file having new passwords
These steps encompass the process of how to change a PDF password using Python. First, we need to check if the file is encrypted or not due to a password for which we use the PdfFileInfo class object. Once the encryption is confirmed, the PdfFileSecurity class object is used to bind and change the password.
Code to Change PDF Password using Python
This code demonstrates the process of how to change password of PDF file using Python. It uses the PdfFileInfo class object in the pdf.facades namespace that has is_encypted property for checking the encryption state of the target PDF file. In the final steps, the PdfFileSecurity class is used to bind and change the password where existing owner password is required in the first place, followed by a new user password, new owner password, output DocumentPrivilege privilege as the print, and encryption key size.
This article has taught us how to change password for PDF using Python. If you want to learn the process to create a PDF file, refer to the article on how to create PDF in Python.