June 27, 2024
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.
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.
Open Terminal and navigate to your project directory:
shcd /path/to/your/directory
Create a virtual environment named pdf-unlocker-env:
shpython3 -m venv pdf-unlocker-env
Activate the virtual environment:
shsource pdf-unlocker-env/bin/activate
We will use the pikepdf library to unlock the PDFs. With the virtual environment activated, install pikepdf:
shpip install pikepdf
Create a Python script named unlock_pdfs_in_folder.py in your project directory:
shtouch unlock_pdfs_in_folder.py
Open unlock_pdfs_in_folder.py with your preferred text editor and add the following code:
pythonimport 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)
To unlock all PDF files in a folder, run the script with the folder path and the password as arguments:
shpython unlock_pdfs_in_folder.py /path/to/folder yourpassword
unlock_pdf function takes the input PDF path, output PDF path, and password to unlock a single PDF file using the pikepdf library.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.After you're done, you can deactivate the virtual environment by running:
shdeactivate