5 Best Ways to Crack PDF Files in Python

Rate this post

πŸ’‘ Problem Formulation: Users may need to unlock or crack PDFs in Python for various legitimate reasons including data retrieval, analysis, or migrating content to a different format. This article will outline how to decrypt and access text from secured PDF files. An example of input would be a password-protected PDF, and the desired output is the text content of that PDF made available for manipulation or extraction in Python.

Method 1: Using PyPDF2 to Unlock PDFs

PyPDF2 is a pure-Python library built as a PDF toolkit. It is capable of extracting document information, splitting documents, merging documents, and, importantly, decrypting password-protected files. To use this method, you need the password of the PDF file.

Here’s an example:

from PyPDF2 import PdfFileReader

def unlock_pdf(file_path, password):
    pdf_file = PdfFileReader(open(file_path, "rb"))
    if pdf_file.isEncrypted:
        print('The file is unlocked!')
        # Do something with the unlocked file
        print('File is not encrypted.')

unlock_pdf('example.pdf', 'your_password_here')

Output when the file is unlocked:

The file is unlocked!

If you have the correct password, the snippet above will unlock the PDF and allow you to read its content. Note that distributing cracked PDFs without authorization can be illegal and is discouraged.

Method 2: Using PyMuPDF to Access Secured PDFs

PyMuPDF is another powerful library that provides fast access to the PDF’s content. While similar to PyPDF2, PyMuPDF offers more features and a faster rendering engine, which can be beneficial for large or complex documents.

Here’s an example:

import fitz  # PyMuPDF

def decrypt_pdf(path, password):
    pdf = fitz.open(path, password=password)
    if pdf.needsPass:
        raise ValueError("Incorrect password")
        print('PDF unlocked successfully')
        # Proceed with operations on unlocked PDF

decrypt_pdf('locked.pdf', 'secret')

Output if the password is correct:

PDF unlocked successfully

After unlocking the PDF with the correct password, you can safely extract text or manipulate the file’s content. Remember to adhere to copyright and ethical considerations.

Method 3: Using pdfrw to Unsecure a PDF File

The pdfrw library can read and write PDF files, and also merge, split, and rotate pages. It’s another tool that can be used for decrypting PDFs, provided you have the password.

Here’s an example:

from pdfrw import PdfReader, PdfWriter

def unsecure_pdf(input_path, output_path, user_pwd):
    reader = PdfReader(input_path, decrypt=user_pwd)
    writer = PdfWriter()

    for page in reader.pages:

    print('PDF has been unsecured')

unsecure_pdf('secured.pdf', 'unsecured_output.pdf', 'userpass')

Output after the process:

PDF has been unsecured

This code shows how to open a secured PDF with pdfrw, copy all its pages into a new document, and then save it as an unencrypted PDF. Always ensure you have permission to modify and distribute the document’s content.

Method 4: Using the qpdf Command-line Tool Through Python

Aside from pure Python libraries, Python can also run command-line tools like qpdf that are designed to inspect, transform and repair PDF files. Qpdf includes functionality for decrypting PDFs which can be accessed through Python’s subprocess module.

Here’s an example:

import subprocess

def qpdf_decrypt(input_pdf, output_pdf, password):
    command = f"qpdf --decrypt --password={password} {input_pdf} {output_pdf}"
    result = subprocess.run(command, shell=True, text=True, capture_output=True)
    if result.returncode == 0:
        print('PDF decryption successful')
        print('PDF decryption failed')

qpdf_decrypt('example_encrypted.pdf', 'example_decrypted.pdf', 'pass')

Output if the decryption is successful:

PDF decryption successful

Using the qpdf command line invoked within Python allows you to leverage a tool specifically built for PDF manipulations. This method requires having qpdf installed on your system and proper permissions for the file.

Bonus One-Liner Method 5: Using One-Liners with PyPDF2

In a pinch and need a quick solution? If one-liners are your thing and you just need a quick unlock, PyPDF2 can help with a compact code snippet.

Here’s an example:

from PyPDF2 import PdfFileReader, PdfFileWriter

PdfFileWriter().write('unlocked.pdf', PdfFileReader(open('locked.pdf', 'rb')).decrypt('thepassword'))

Output for a successful unlock:

The file 'unlocked.pdf' has been created without password protection.

This one-line command uses PyPDF2 to decrypt a PDF and write an unlocked version to a new file. It’s compact, readable, and efficient for programmers familiar with the concept.


  • Method 1: PyPDF2. Great for straightforward unlocking of PDFs. Limited to the features provided by the library. Speed may vary depending on the PDF file size.
  • Method 2: PyMuPDF. Offers more functionality and faster rendering than PyPDF2, which may be necessary for complex and graphic-heavy PDFs. More difficult to install due to dependencies.
  • Method 3: pdfrw. Good for not just unlocking but also for manipulating PDF files. However, the library is less verbose in error handling, which might be challenging to debug.
  • Method 4: qpdf Command-line Tool. A powerful dedicated tool for PDF manipulations, not just unlocking. Requires separate installation. Not ideal for environments where installing extra software is not allowed.
  • Bonus Method 5: One-Liner with PyPDF2. Quick and easy for those already familiar with Python and PyPDF2. Not as detailed in terms of error messages and not suitable for complex manipulations.