PDF (Portable Document Format) is one of the most used file formats for storing and sending documents. They are commonly used for many purposes such as eBooks, Resumes, Scanned documents, etc. But as we share pdf to many people, there is a possibility of its data getting leaked or stolen. So, it’s necessary to password protect our PDF files so that only authorized persons can have access to it.
In this article, we are going to see how can we set a password to protect a PDF file. We’ll be using the PyPDF2 module to encrypt and decrypt our PDF files. PyPDF2 is a Python library built as a PDF toolkit. It is capable of:
- Extracting document information (title, author, …)
- Splitting and Merging documents
- Cropping pages
- Encrypting and decrypting PDF files
PyPDF2 is not an inbuilt library, so we have to install it.
pip3 install PyPDF2
Now, we are ready to write our script to encrypt PDF files.
Encrypting the PDF File
First, We will open our PDF file with the reader object. Then, we will create a copy of the original file so that if something goes wrong, it doesn’t affect our original file. To create a copy, we have to iterate through every page of the file and add it to our new PDF file. Then, we can simply encrypt our new PDF file.
PDF File used:
This will create a copy of the original file and encrypt it with the entered password. Once the PDF is encrypted, it can not be opened without entering the correct password.
Decrypting The PDF File
But what if we want to decrypt the encrypted PDF file? We can do this too with this library. The process is almost the same. We will open the encrypted file with the correct password and create a copy of it by iterating through every page of it and adding it to our new PDF file.
Here’s the code:
This will create a copy of the encrypted file that doesn’t require a password to be opened.
So, this was a basic script to encrypt and decrypt PDF files. But there are a plethora of ideas to extend this. You can create a GUI tool to do it or Develop a Web application that encrypts PDF files. You can also create a whole PDF Manager using the PyPDF2 library.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Encrypt and Decrypt Image using Python
- Send PDF File through Email using pdf-mail module
- Encrypt using XOR Cipher with Repeating Key
- Encrypt the String according to the given algorithm in Python
- Convert Text and Text File to PDF using Python
- Build an Application to extract URL and Metadata from a PDF using Python
- Python | Convert Image to Pdf using img2pdf module
- Python | Reading contents of PDF using OCR (Optical Character Recognition)
- Exporting PDF Data using Python
- Extract text from PDF File using Python
- Merge PDF stored in Remote server using Python
- PDF Redaction using Python
- Convert PDF to Image using Python
- Convert PDF File Text to Audio Speech using Python
- Add Watermark to PDF using PyPDF4 in Python
- Modifying PDF file using Python
- Python Convert Html to PDF
- Python | Scipy stats.halfgennorm.pdf() method
- Python | Scipy stats.hypsecant.pdf() method
- Convert Docx to Pdf usinf docx2pdf Module in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.