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.


123 views0 comments

Recent Posts

See All

How to encrypt and decrypt files in Python When you need to encrypt text, it is important to understand the different options you have. There are different ways to encrypt text, like using a password,

### 👽⚠️ 𝗧𝗵𝗶𝘀 𝘀𝗰𝗿𝗶𝗽𝘁 𝗶𝘀 𝗷𝘂𝘀𝘁 𝘁𝗵𝗲 𝘁𝗼𝗼𝗹 𝗳𝗼𝗿 𝗲𝗱𝘂𝗰𝗮𝘁𝗶𝗼𝗻𝗮𝗹 𝗮𝗻𝗱 𝘁𝗲𝘀𝘁𝗶𝗻𝗴 𝗽𝘂𝗿𝗽𝗼𝘀𝗲𝘀 ⚠️👽 ### Descriptions: This is script for spamming the message from Wh

XML (Extensible Markup Language) is a markup language similar to HTML, but without predefined tags to use. Instead, you define your own tags designed specifically for your needs. This is a powerful wa