From 8a7e006de811735a3b4ab3ab3c5c6b0bf12a4254 Mon Sep 17 00:00:00 2001 From: Dan Howe Date: Wed, 28 Mar 2018 07:23:01 +1100 Subject: [PATCH] Initial commit --- scripts/pdf_unlock.py | 65 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 scripts/pdf_unlock.py diff --git a/scripts/pdf_unlock.py b/scripts/pdf_unlock.py new file mode 100644 index 0000000..cc3221d --- /dev/null +++ b/scripts/pdf_unlock.py @@ -0,0 +1,65 @@ +__author__ = "D. Howe" +__version__ = "0.1.0" +__email__ = "d.howe@wrl.unsw.edu.au" + +import os +import sys +import glob +import argparse +import subprocess + + +def pdf_unlock(): + # Get pdfs in same folder + fnames = [ + os.path.join(name) for name in os.listdir('.') if name.endswith('.pdf') + ] + + # Use pdf names from user (if provided) + if len(sys.argv[1:]) > 0: + fnames = sys.argv[1:] + + for fname in fnames: + + # Strip file extension + base_name = os.path.splitext(fname)[0] + + # save as temporary file + cmd_str = ('gs ' + '-dNOPAUSE ' + '-dQUIET ' + '-dBATCH ' + '-sDEVICE=pdfwrite ' + '-sOutputFile="{output_pdf}" ' + '"{input_pdf}"').format(output_pdf, input_pdf) + + print('Processing ' + base_name + '.pdf ...') + + with subprocess.Popen( + cmd_str, + stdout=subprocess.PIPE, + bufsize=1, + universal_newlines=True) as p: + for line in p.stdout: + print(line, end='') + + # Overwrite original file + os.remove(base_name + '.pdf') + os.rename(base_name + '_temp.pdf', base_name + '.pdf') + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('folder', help='name of input folder', default=None) + parser.add_argument( + '-r', + '--recursive', + help='search for files recursively', + action='store_true') + args = parser.parse_args() + + print(args.recursive) + + +if __name__ == '__main__': + main()