OWolf

2024-09-30 Web Development

How to Unlock Multiple PDF Files in a Folder Using Python

By O Wolfson

Unlocking multiple PDF files individually can be a tedious task, especially if you have a large number of them. Fortunately, Python provides an efficient way to automate this process. Let's create a Python script that unlocks all PDF files in a specified folder and saves the unlocked versions with a modified filename.

Step 1: Set Up a Virtual Environment

To ensure a clean and isolated environment for our project, we will use a virtual environment. This is especially useful on a Mac to avoid conflicts with system-wide packages.

  1. Open Terminal and navigate to your project directory:

    sh
    cd /path/to/your/directory
    
  2. Create a virtual environment named pdf-unlocker-env:

    sh
    python3 -m venv pdf-unlocker-env
    
  3. Activate the virtual environment:

    sh
    source pdf-unlocker-env/bin/activate
    

Step 2: Install Required Package

We will use the pikepdf library to unlock the PDFs. With the virtual environment activated, install pikepdf:

sh
pip install pikepdf

Step 3: Create the Python Script

Create a Python script named unlock_pdfs_in_folder.py in your project directory:

sh
touch unlock_pdfs_in_folder.py

Open unlock_pdfs_in_folder.py with your preferred text editor and add the following code:

python
import pikepdf
import os
import sys

def unlock_pdf(input_pdf_path, output_pdf_path, password):
    try:
        with pikepdf.open(input_pdf_path, password=password) as pdf:
            pdf.save(output_pdf_path)
        print(f"Unlocked PDF saved as: {output_pdf_path}")
    except pikepdf._qpdf.PasswordError:
        print(f"Incorrect password for {input_pdf_path}.")
    except Exception as e:
        print(f"An error occurred with {input_pdf_path}: {e}")

def unlock_pdfs_in_folder(folder_path, password):
    for filename in os.listdir(folder_path):
        if filename.endswith(".pdf"):
            input_pdf_path = os.path.join(folder_path, filename)
            output_pdf_path = os.path.join(folder_path, f"{os.path.splitext(filename)[0]}-ul.pdf")
            unlock_pdf(input_pdf_path, output_pdf_path, password)

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python unlock_pdfs_in_folder.py <folder_path> <password>")
    else:
        folder_path = sys.argv[1]
        password = sys.argv[2]
        unlock_pdfs_in_folder(folder_path, password)

Step 4: Run the Script

To unlock all PDF files in a folder, run the script with the folder path and the password as arguments:

sh
python unlock_pdfs_in_folder.py /path/to/folder yourpassword

Explanation of the Script

  1. Unlock a Single PDF: The unlock_pdf function takes the input PDF path, output PDF path, and password to unlock a single PDF file using the pikepdf library.
  2. Process Multiple PDFs: The unlock_pdfs_in_folder function iterates over all files in the specified folder, checking if they have a .pdf extension. If so, it constructs the input and output paths and calls unlock_pdf.
  3. Command-Line Arguments: The script accepts two command-line arguments: the folder path containing the PDFs and the password to unlock them.

Deactivate the Virtual Environment

After you're done, you can deactivate the virtual environment by running:

sh
deactivate