How to Encrypt and Decrypt PDF Files with Python

Alice Yang
5 min readJul 11, 2024

--

In today’s digital age, PDF documents have become an essential part of our personal and professional lives. From confidential business reports to sensitive personal records, PDFs are ubiquitous in storing and sharing important information. However, with the increasing risk of data breaches and unauthorized access, it is crucial to ensure the security of these PDF files. To this end, this blog will explore how to encrypt and decrypt PDF files using Python.

We will explore the following topics:

Python Library to Encrypt and Decrypt PDF Files

To encrypt and decrypt PDF files in Python, we can use Spire.PDF for Python, which is a multifunctional library designed to create, read, manipulate, and convert PDF files within Python applications.

You can install Spire.PDF for Python from PyPI using the following pip command:

pip install Spire.Pdf

If you already have Spire.PDF for Python installed and would like to upgrade to the latest version, use the following pip command:

pip install --upgrade Spire.Pdf

For more detailed information about the installation, check this official documentation: How to Install Spire.PDF for Python.

Encrypt a PDF with Python

To encrypt a PDF, you need to set a user password (open password) that will be required to open and view the file.

What is a user password (open password)?

  • It is the password that users must enter to open and view the PDF document.
  • It restricts access to the PDF content, ensuring that only authorized individuals can access the file.
  • The user password does not provide any additional permissions or restrictions beyond granting access to open the PDF.

In addition to setting a user or open password, you also need to set the encryption level or algorithm that will be used to secure the contents of the PDF. Spire.PDF for Python supports the following encryption levels or algorithms:

  • 40-bit RC4
  • 128-bit RC4
  • 128-bit AES
  • 256-bit AES

The main steps to encrypt a PDF file using Python are as follows:

  • Create a PdfDocument instance and load a PDF file using PdfDocument.LoadFromFile() method.
  • Create a PdfSecurityPolicy and set the user password required to open the document.
  • Set the encryption algorithm to use for securing the PDF through the EncryptionAlgorithm property in the PdfSecurityPolicy class.
  • Apply the security policy to encrypt the PDF document using PdfDocument.Encrypt(securityPolicy:PdfSecurityPolicy) method.
  • Save the encrypted PDF document to a new file.

The following code example shows how to encrypt a PDF file using Python:

from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
pdf = PdfDocument()
# Load an existing PDF file that needs to be encrypted
pdf.LoadFromFile("Sample.pdf")

# Create a security policy and set the user password required to open the document
securityPolicy = PdfPasswordSecurityPolicy("userpassword", str())

# Specify the encryption algorithm to use for securing the PDF
securityPolicy.EncryptionAlgorithm = PdfEncryptionAlgorithm.AES_256

# Apply the security policy to encrypt the PDF document
pdf.Encrypt(securityPolicy)

# Save the encrypted PDF document to a new file
pdf.SaveToFile("Encrypt.pdf")
pdf.Close()
Encrypt or Password Protect PDF with Python
Encrypt or Password Protect PDF with Python

Set Security Permissions in a PDF with Python

To set the security permissions for a PDF document, you need to set an owner password (permissions password) in addition to the user password.

What is an owner password (permissions password)?

  • It is used to control the permissions and actions that can be performed on the PDF document.
  • It allows the document owner to restrict certain operations, such as printing, copying, modifying, or extracting content from the PDF.
  • The owner password is typically more powerful than the user password, as it grants the owner full control over the PDF’s security settings.

The main steps to set security permissions in a PDF using Python are as follows:

  • Create a PdfDocument instance and load a PDF file using PdfDocument.LoadFromFile() method.
  • Create a PdfSecurityPolicy and set the user password to open the document and the owner password to restrict permissions.
  • Set the encryption algorithm to use for securing the PDF through the EncryptionAlgorithm property in the PdfSecurityPolicy class.
  • Restrict all permissions and then only allow certain permissions through the DocumentPrivilege property in the PdfSecurityPolicy class.
  • Apply the security policy to encrypt the PDF document using PdfDocument.Encrypt(securityPolicy:PdfSecurityPolicy) method.
  • Save the encrypted PDF document to a new file.

The following code example shows how to set security permissions in a PDF using Python:

from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
pdf = PdfDocument()
# Load an existing PDF file that needs to be encrypted
pdf.LoadFromFile("Sample.pdf")

# Create a security policy and set the user password to open the document and the owner password to restrict permissions
securityPolicy = PdfPasswordSecurityPolicy("userpassword", "ownerpassword")

# Set the encryption algorithm
securityPolicy.EncryptionAlgorithm = PdfEncryptionAlgorithm.AES_256

# Restrict all permissions
securityPolicy.DocumentPrivilege = PdfDocumentPrivilege.ForbidAll()
# Only allow printing of the document
securityPolicy.DocumentPrivilege.AllowPrint = True

# Encrypt all document content except metadata (optional, it allows search engines to access the document’s metadata)
# securityPolicy.EncryptMetadata = False

# Apply the security policy to encrypt the PDF document
pdf.Encrypt(securityPolicy)

# Save the encrypted PDF document to a new file
pdf.SaveToFile("EncryptWithPermissions.pdf")
pdf.Close()
Set Security Permissions in PDF with Python
Set Security Permissions in PDF with Python

Decrypt a PDF with Python

An encrypted PDF file can be opened with either its user password or owner password. Once it has been opened, you can utilize the PdfDocument.Decrypt() method to decrypt it.

The main steps to decrypt an encrypted PDF file using Python are as follows:

  • Create a PdfDocument instance and load an encrypted PDF file with either its user password or owner password using PdfDocument.LoadFromFile() method.
  • Call PdfDocument.Decrypt() method to decrypt the PDF file.
  • Save the decrypted PDF file to a new file.

The following code example shows how to decrypt an encrypted PDF file using Python:

from spire.pdf.common import *
from spire.pdf import *

# Create a PdfDocument object
pdf = PdfDocument()
# Load an encrypted PDF document with either its user password or owner password
pdf.LoadFromFile("EncryptWithPermissions.pdf", "userpassword")

# Decrypt the PDF document
pdf.Decrypt("ownerpassword")

# Save the resulting document to a new file
pdf.SaveToFile("Decrypt.pdf")
pdf.Close()

Conclusion

This blog post demonstrated how to encrypt a PDF file, set security permissions for a PDF file, and decrypt a PDF file using Python. We hope you find it helpful.

Related Topics

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.