Skip to content

Instantly share code, notes, and snippets.

@0xmycf
Last active February 9, 2023 13:29
Show Gist options
  • Save 0xmycf/a3bab87ba6d1bdea9b01d179a89105c1 to your computer and use it in GitHub Desktop.
Save 0xmycf/a3bab87ba6d1bdea9b01d179a89105c1 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3
import pikepdf as ppdf
import os
import os.path as op
import sys
def main(dir: str, is_verbose: bool) -> int:
if not dir.endswith("/"):
dir += "/"
# if its not a directory, we abort
if not op.isdir(dir):
print("Please enter a valid directory")
return 1
for file in os.listdir(dir):
file_path = dir + file
if op.isfile(file_path) and file.endswith(".pdf"):
pw = file.removesuffix(".pdf").strip()
with ppdf.open(file_path, password=pw) as iofile:
saving_dest = dir + pw + '-nopw' + '.pdf'
if not op.isfile(saving_dest):
if is_verbose:
print("Saving file to: " + saving_dest)
iofile.save(saving_dest)
if is_verbose:
print("File has been saved to: " + saving_dest)
elif is_verbose:
print(saving_dest + " Already exsits. Aborting save.")
elif is_verbose:
print(file + " Is not a file or does not end with '.pdf'. Ignoring.")
return 0
if __name__ == '__main__':
is_verbose = len(sys.argv) > 1 and sys.argv[1] == "-v"
dir = input("Give the source dir: ")
exit_code = main(dir, is_verbose)
sys.exit(exit_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment