summaryrefslogtreecommitdiff
path: root/a6_book.py
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-10-19 08:36:06 +0200
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2021-10-19 08:36:06 +0200
commitf43b07b20a416387ebd867774e88e0129ac9f06b (patch)
tree1f78cf05886e9a1a26c3c1537832984fecec1e20 /a6_book.py
Impose a pdf on an A6 booklet with 16 pages signature
Diffstat (limited to 'a6_book.py')
-rwxr-xr-xa6_book.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/a6_book.py b/a6_book.py
new file mode 100755
index 0000000..189018f
--- /dev/null
+++ b/a6_book.py
@@ -0,0 +1,66 @@
+#!/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,
+)
+
+
+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 '<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()
+
+ 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', '2x2',
+ #'--no-tidy',
+ '-o', out_fname,
+ args.file,
+ ','.join(rearranged)
+ ])
+
+
+if __name__ == "__main__":
+ main()