summaryrefslogtreecommitdiff
path: root/page_manipulation/a6_book.py
blob: cbb9d44e97aadb259cfabed4f73210bebb01ecaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3

import argparse
import os.path
import subprocess

try:
    import pypdf
except ImportError:
    pypdf = None


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,
)

SIGNATURE_64 = (
    63,  0, 61,  2,  1, 62,  3, 60,
    59,  4, 57,  6,  5, 58,  7, 56,
    55,  8, 53, 10,  9, 54, 11, 52,
    51, 12, 49, 14, 13, 50, 15, 48,
    47, 16, 45, 18, 17, 46, 19, 44,
    43, 20, 41, 22, 21, 42, 23, 40,
    39, 24, 37, 26, 25, 38, 27, 36,
    35, 28, 33, 30, 29, 34, 31, 32,
)


def get_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--double", "-d",
        action="store_true",
        help="Put two signatures on 4 A4 sheet. Pages will be a "
        "multiple of 32",
    )
    parser.add_argument(
        "--quadruple", "-q",
        action="store_true",
        help="Put four signatures on 8 A4 sheet. Pages will be a "
        "multiple of 64",
    )
    parser.add_argument(
        "--pagespec",
        help="Page selection. At the moment only '<start>-<end>' is supported",
        default=None,
    )
    parser.add_argument(
        "file",
        help="Name of the PDF file",
    )
    return parser


def main():
    parser = get_parser()
    args = parser.parse_args()


    if not args.pagespec:
        if not pypdf:
            print("Detecting the number of pages in the pdf requires pypdf")
            sys.exit(1)
        reader = pypdf.PdfReader(args.file)
        start = 1
        end = end = len(reader.pages)
    else:
        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
    elif args.quadruple:
        signature = SIGNATURE_64
    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()