You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.6 KiB
Python
66 lines
1.6 KiB
Python
__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()
|