'Looking to create a script to decrypt password protected PDF files

I have multiple directories of password protected PDF files and I'm looking to decrypt all of these files and save the resulting decrypted file as a new file in the same folder as the encrypted file. I would like to create a scripted solution to accomplish this. It doesn't really matter if the solution is Python, Powershell, bash, etc. but I've been using Windows to date to run a tool called qpdf to accomplish this in a manual way on 1 file at a time. I have already added the extracted qpdf directory to my path, so to run I can simply navigate to a directory, then run the following to decrypt 1 pdf file and save it as desired.

> qpdf --decrypt --password=filepasswordhere EncryptedPDFFilename DecryptedPDFFilename

Note that these files have the same password (which leads me to believe this shouldn't be too crazy to script out). Any help, suggestions, push in the the right direction would be greatly appreciated.



Solution 1:[1]

you start with the command line you already use

qpdf --decrypt --password=filepasswordhere EncryptedPDFFilename DecryptedPDFFilename

Then run for /? at that cmd console to check what the following cmd line may do then work it from there

So simply write a one line text file to run in the topmost folder, to call qpdf.exe directly (rather than depend its always in path), but do use a small sub set of files for testing first, note the %%a needs to doubled when run inside a file.cmd but are just %a if using cmd console direct.

test.cmd

for /R %%a in (*.pdf) do "C:\path to qpdf-10.6.3\bin\qpdf.exe" --decrypt --password=filepasswordhere "%%a" "%%~dpna-decrypted.pdf"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1