top of page

Add/Remove PDF Password Using Python

Here I am going to show you an example how to encrypt PDF to make it password protected using PyPDF2 module in Python programming language. Piece of cake 🍰

This example we are going to read the existing PDF file and making this PDF file password protected.

It is a pretty simple idea to make the PDF password protected when you want to send some sensitive data into the PDF file over the network, such as, email or you are allowing end users to download the PDF files.


- Python 3.9.1

- PyPDF2 1.26.0


pip install pypdf2

The code:

import PyPDF2
pdfFile = open('example.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)
pdfWriter = PyPDF2.PdfFileWriter()
for pageNum in range(pdfReader.numPages):
resultPdf = open('encrypted_output.pdf', 'wb')


Password Remove:


pip install pikepdf

The code:

import pikepdf

pdf_loc = input("PDF location: ")
pdf_pass = input("PDF password: ")

pdf =, password=pdf_pass)


pdf_save = input("Save file as: ")
pdf_loc2 = input("Save location: ") + '\\' + pdf_save)

print("The password successfully removed from the PDF")
print("\aLocation: " + pdf_loc + '\\' + pdf_save)


Congratulations, right now, you can open PDF files without being bothered with the password.

902 views0 comments

Recent Posts

See All

Mobile | Secret Phone Codes

Our smartphones have become extensions of ourselves, facilitating much of our communication and storing troves of private data. If someone wants to monitor your activity, tapping your phone is a viabl

Mouse & Keyboard Bot

This article illustrates how to automate mouse and keyboard movements using pyautogui module in python. This module is not preloaded with python. So to install it run the following command: The code s

Edit a Website with a Java console

Edit a website – even if it’s not yours! This is how you can edit any website directly from your browser: #1) Open a Webpage; #2) Right-click on any spot in the website, select “Inspect Element”; #3)


bottom of page