file-setup/file_setup

62 lines
1.6 KiB
Plaintext
Raw Normal View History

2020-04-03 16:35:39 +00:00
#!/usr/bin/env python3.5
# coding=utf-8
"""
merge_images
Usage: For usage instructions run with option --help
Author: Stephan Porada <sporada@uni-bielefeld.de>
"""
from argparse import ArgumentParser
import os
import subprocess
def parse_arguments():
parser = ArgumentParser(description='Merges images into one .pdf file.')
parser.add_argument('i', metavar='input directory')
parser.add_argument('o', metavar='output directory')
parser.add_argument('f', metavar='output file base')
parser.add_argument('--log-dir')
parser.add_argument('--zip')
return parser.parse_args()
def merge_images(input_dir, output_dir, output_file_base, zip):
try:
os.mkdir(output_dir)
except FileExistsError:
pass
cmd = 'ls -Q -v "{i}"/*.* > "{i}"/file_list.txt'.format(i=input_dir)
subprocess.run(cmd, shell=True)
cmd = 'convert @"{}"/file_list.txt "{}"/"{}".pdf'.format(input_dir, output_dir, output_file_base) # noqa
subprocess.run(cmd, shell=True)
cmd = 'rm "{}"/file_list.txt'.format(input_dir)
subprocess.run(cmd, shell=True)
if zip is not None:
2020-04-04 13:37:29 +00:00
# Remove .zip file extension if provided
if zip.endswith('.zip'):
zip = zip[:-4]
zip = zip if zip else 'output'
2020-04-03 16:35:39 +00:00
cmd = 'cd "{}"'.format(output_dir)
cmd += ' && '
cmd += 'zip'
cmd += ' -m'
2020-04-04 13:37:29 +00:00
cmd += ' "{}" "{}".pdf'.format(zip, output_file_base)
2020-04-03 16:35:39 +00:00
cmd += ' && '
cmd += 'cd -'
subprocess.run(cmd, shell=True)
def main():
args = parse_arguments()
print(args)
merge_images(args.i, args.o, args.f, args.zip)
if __name__ == '__main__':
main()