How to Encrypt or Password Protect a PDF file in Python (Script, Source code)

PDFs are a great way to share information, but they can be problematic if you need to protect the information contained within them. That’s where encryption comes in. By encrypting a PDF, you can prevent anyone from being able to open it without a password. In this blog post, we’ll show you how to encrypt or password-protect a PDF in Python.

Encrypt or Password Protect a PDF file using Python

Step 1: Import Libraries

In order to encrypt a PDF, you’ll need to first import a PyPDF2 .library into your Python script. PyPDF2 gives you the ability to read and write PDFs.

Step 2: Choose a File to Encrypt

Next, you’ll need to specify which PDF you want to encrypt. Make sure that this file is saved in the same directory as your Python script.

Step 3: Create Encryption Settings

Now, you’ll need to create an encryption setting object. This object will specify what type of encryption you want to use as well as the password that will be required to open the encrypted PDF. In the code below, we’ve chosen 128-bit encryption with the password “secret.” However, feel free to change these settings as you see fit.

Step 4: Open PDF

With your encryption settings object in place, you can now open your PDF and begin the encryption process. To do so, simply replace the ENTER FILENAME HERE placeholder in the code below with the name of your PDF file.

pdfReader = PyPDF2.PdfFileReader(open('ENTER FILENAME HERE', 'rb'))
Code language: JavaScript (javascript)

Step 5: Encrypt PDF

Finally, it’s time to encrypt your PDF! To do so, simply run the following line of code:

Code language: JavaScript (javascript)

And that’s it! Your PDF has now been encrypted and can only be opened by those who know the password.

A full Python Script to encrypt your PDF

# PyPDF2 helps to read, write and many other functionalities with PDF's # Install PyPDF2 if not in your system. # Use `pip install PyPDF2` command to install the package from pip installer. from PyPDF2 import PdfFileWriter, PdfFileReader print("The file should be in same FOLDER as this script") pdf_File_Name = input("Enter EXACT name of the PDF in this FOLDER: ") pdf_File = open(pdf_File_Name + ".pdf",'rb') # Reading the pdf pdf = PdfFileReader(pdf_File) # Object for writing the file write_Obj = PdfFileWriter() # Getting the number of pages and writing each page in the writer object for i in range(pdf.getNumPages()): page = pdf.getPage(i) write_Obj.addPage(page) # Encrypting by the password owner_Password = input("Enter Password for OWNER: ") user_Password = input("Enter Password for USER: ") write_Obj.encrypt(user_pwd=user_Password, owner_pwd=owner_Password, use_128bit=True) # Naming and creating the encrypted PDF new_PDF_Name_Input = input("Enter new ENCRYPTED PDF name: ") new_PDF_Name = new_PDF_Name_Input + '.pdf' encrypted_PDF = open(new_PDF_Name, 'wb') write_Obj.write(encrypted_PDF)
Code language: Python (python)

Download Source code

The most important part of the code above is the encrypt() function, which asks for a password that will be used later to open the PDF file.

encrypt(user_password, owner_password=None, use_128bit=True)
Code language: PHP (php)


  • user_password(str) – The user password, which allows you to open and read the PDF file with the restrictions provided.
  • owner_password(str) – The owner password, which allows you to open PDF files without any restrictions. By default, the owner password is the same as the user password.
  • use_128bit(bool) – flag as to whether to use 128bit encryption. When false, 40bit encryption will be used. By default, this flag is on.

Read more: How To Encrypt PDF As Password Protected File In Python

Setup Instructions

Download and install Python if not in your local system.

Download the required packages

$pip install PyPDF2
Code language: PHP (php)

To run the script you need to use the following command

Code language: PHP (php)

Make sure that the PDF to be encrypted is in that same folder as this python code file.

That’s all there is to encrypting or password-protecting a PDF in Python! Using either the PyPDF2 library makes it easy to keep your documents safe from prying eyes. Do you have any questions about this post? Let us know in the comments below!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts