Code for How to Crack PDF Files in Python

You can also view the full code on github.

import pikepdf
from tqdm import tqdm

# load password list
passwords = [ line.strip() for line in open("wordlist.txt") ]

# iterate over passwords
for password in tqdm(passwords, "Decrypting PDF"):
        # open PDF file
        with"foo-protected.pdf", password=password) as pdf:
            # Password decrypted successfully, break out of the loop
            print("[+] Password found:", password)
    except pikepdf._qpdf.PasswordError as e:
        # wrong password, just continue in the loop