In this article, we will see how to remove a password from a PDF using Python and the PyPDF2 library. By following these simple steps, you can easily access password-protected PDFs and make them available to everyone.

To proceed further, you need the following two things.

  1. Python 3 installed on your computer
  2. The PyPDF2 library. You can install it by running the following command in your terminal or command prompt:
pip install PyPDF2

Import the PyPDF2 library:

To begin, you must import the PyPDF2 library into your Python script. This library contains a set of functions and methods for working with PDF files in Python. The following is the code to import the library:

import PyPDF2

Opening the file

The following step is to open the PDF file from which you want to remove the password. You can accomplish this by utilising the open function, which is part of Python’s built-in file handling capabilities. You should pass the file name as a string to the open function, and use the ‘rb’ argument to specify that you want to read the file as binary. Following is the code to open a file:

pdfFile = open('protected.pdf', 'rb')

PDF Reader Object

Using the PdfFileReader function from the PyPDF2 library, we can now create a PDF reader object. This function takes the open file as an argument and returns a PDF reader object that you can use to access the PDF file’s contents. The code for creating the PDF reader object is as follows:

pdfReader = PyPDF2.PdfFileReader(pdfFile)

Check if PDF has password

Before removing the password from the PDF file, double-check that it is indeed password protected. You can do this by inspecting the PDF reader object’s isEncrypted property. If this property has the value True, the PDF file is password protected. Here’s the code for checking whether the PDF is password protected:

if pdfReader.isEncrypted:
    print('The PDF is password protected.')
else:
    print('The PDF is not password protected.')

Removing the password

You can remove the password now that you’ve confirmed that the PDF file is password protected. You will do this by calling the decrypt method of the PDF reader object, which accepts the password as an argument.

If the password is correct, the method will remove the password protection and allow you to view the PDF file’s contents. If the password is incorrect, the method will fail.

pdf.decrypt("password")

Finally, After you have removed the password from the PDF file, use the close() method to close it. This ensures that the file is properly saved and that the changes you made to the file are saved.

pdfFile.close()

Full Code:

import PyPDF2
pdfFile = open('protected.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFile)

if pdfReader.isEncrypted:
    print('The PDF is password protected.')
else:
    print('The PDF is not password protected.')

pdf.decrypt("password")
pdfFile.close()

Categorized in:

Tagged in: