From 3c648af33c4ebb04b369d5c57efe67de5d1f3ef6 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Fri, 8 Jul 2022 15:29:07 +0200 Subject: Script to impose A5 pages on A4 --- a5_book.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 a5_book.py diff --git a/a5_book.py b/a5_book.py new file mode 100755 index 0000000..846ca86 --- /dev/null +++ b/a5_book.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +import argparse +import os.path +import subprocess + + +SIGNATURE = ( + 15, 0, + 1, 14, + 13, 2, + 3, 12, + 11, 4, + 5, 10, + 9, 6, + 7, 8 +) + + +def get_parser(): + parser = argparse.ArgumentParser() + parser.add_argument( + "--file", '-f', + help="Name of the PDF file", + ) + parser.add_argument( + "pagespec", + help="Page selection. At the moment only '-' 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() + + pages = [str(i) for i in range(start, end+1)] + if len(pages) % 16 != 0: + empty = 16 - len(pages) % 16 + pages = pages + ["{}"] * empty + + rearranged = [] + while pages: + for i in SIGNATURE: + rearranged.append(pages[i]) + pages = pages[16:] + + base, ext = os.path.splitext(args.file) + out_fname = base + '-book' + ext + + subprocess.run([ + 'pdfjam', + '--nup', '2x1', + "--landscape", + #'--no-tidy', + '-o', out_fname, + args.file, + ','.join(rearranged) + ]) + + +if __name__ == "__main__": + main() -- cgit v1.2.3