Skip to content

Instantly share code, notes, and snippets.

How to Change Password of PDF using Python. For more details: https://kb.aspose.com/pdf/python/how-to-change-password-of-pdf-using-python/
import aspose.pdf as pdf
# Load the license
license = pdf.License()
license.set_license("Aspose.Total.lic")
# Create PdfFileInfo object
pdfFileInfo = pdf.facades.PdfFileInfo("PasswordPDF.pdf")
# Create PdfFileSecurity object
if pdfFileInfo.is_encrypted == True:
# Create PdfFileSecurity object
fileSecurity = pdf.facades.PdfFileSecurity()
# Bind the source PDF file with the PdfFileSecurity object
fileSecurity.bind_pdf("PasswordPDF.pdf")
# Change password
fileSecurity.change_password("owner", "new_user", "new_owner", pdf.facades.DocumentPrivilege.print, pdf.facades.KeySize.X256)
# Save PDF with new user and owner passwords
fileSecurity.save("UpdatedPasswordPDF.pdf")
print("Password changed successfully")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment