diff options
author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2023-08-12 10:22:29 +0200 |
---|---|---|
committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2023-08-12 10:22:29 +0200 |
commit | f46c3bbdd29191654b009c26a28cf99a90484fec (patch) | |
tree | bf25adc92cd04a2d81693004fb87af0bfeb18d29 /page_manipulation/a6_book.py | |
parent | 39e31f9231fb7b5493a31c6fa5c7745f4b7fab81 (diff) |
Moved page manipulation scripts into a subdirectory
Diffstat (limited to 'page_manipulation/a6_book.py')
-rwxr-xr-x | page_manipulation/a6_book.py | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/page_manipulation/a6_book.py b/page_manipulation/a6_book.py new file mode 100755 index 0000000..059a717 --- /dev/null +++ b/page_manipulation/a6_book.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 + +import argparse +import os.path +import subprocess + + +SIGNATURE = ( + 15, 0, 13, 2, + 1, 14, 3, 12, + 11, 4, 9, 6, + 5, 10, 7, 8, +) + +SIGNATURE_32 = ( + 15, 0, 31, 16, + 1, 14, 17, 30, + 13, 2, 29, 18, + 3, 12, 19, 28, + 11, 4, 27, 20, + 5, 10, 21, 26, + 9, 6, 25, 22, + 7, 8, 23, 24, +) + + +def get_parser(): + parser = argparse.ArgumentParser() + parser.add_argument( + "--file", '-f', + help="Name of the PDF file", + ) + parser.add_argument( + "--double", "-d", + action="store_true", + help="Put two signatures on 4 A4 sheet. Pages should be a " + "multiple of 32", + ) + parser.add_argument( + "pagespec", + help="Page selection. At the moment only '<start>-<end>' is supported" + ) + return parser + + +def main(): + parser = get_parser() + args = parser.parse_args() + pspec = args.pagespec.split('-') + try: + start = int(pspec[0]) + end = int(pspec[1]) + except ValueError: + parser.print_usage() + except IndexError: + parser.print_usage() + + if args.double: + signature = SIGNATURE_32 + else: + signature = SIGNATURE + sig_len = len(signature) + + pages = [str(i) for i in range(start, end+1)] + if len(pages) % sig_len != 0: + empty = sig_len - len(pages) % sig_len + pages = pages + ["{}"] * empty + + rearranged = [] + + while pages: + for i in signature: + rearranged.append(pages[i]) + pages = pages[sig_len:] + + base, ext = os.path.splitext(args.file) + out_fname = base + '-book' + ext + + subprocess.run([ + 'pdfjam', + '--nup', '2x2', + #'--no-tidy', + '-o', out_fname, + args.file, + ','.join(rearranged) + ]) + + +if __name__ == "__main__": + main() |