-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbooklet_page_list.py
executable file
·127 lines (106 loc) · 3.23 KB
/
booklet_page_list.py
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
126
127
#!/usr/bin/env python
from __future__ import division
import sys
# As per help.ubuntu.com:
# > n, 1, 2, n-1, n-2, 3, 4, n-3, n-4, 5, 6, n-5, n-6, 7, 8, n-7, n-8,
# > 9, 10, n-9, n-10, 11, 12, n-11
# > Examples:
# > 4 page booklet: Type 4,1,2,3
# > 8 page booklet: Type 8,1,2,7,6,3,4,5
# > 20 page booklet: Type 20,1,2,19,18,3,4,17,16,5,6,15,14,7,8,13,12,9,10,11
#
# -<https://help.ubuntu.com/stable/ubuntu-help/
# printing-booklet-duplex.html.en>
"""After using the booklet_order function, you must set the printer's
duplex setting to "Flip on Short Side" and the "Pages" option (may be
under "Range and Copies" depending on your desktop environment) to the
booklet_order. Pages per sheet: 2. Multiple pages per sheet order: Left
to Right, then down.
In this version, only counts divisible by 4 are implemented
completely--you have to cut the page printed last in half otherwise (it
still goes in the middle but make sure you flip it properly.
"""
# n,
# 1, 2,
# n-1, n-2,
# 3, 4,
# n-3, n-4,
# 5, 6,
# n-5, n-6,
# 7, 8,
# n-7, n-8,
# 9, 10,
# n-9, n-10,
# 11, 12,
# n-11
# or:
# n, 1, # b
# 2, n-1, # f
# n-2, 3, # b
# 4, n-3, # f
# n-4, 5, # b
# 6, n-5, # f
# n-6, 7, # b
# 8, n-7, # f
# n-8, 9, # b
# 10, n-9, # f
# n-10, 11, # b
# 12, n-11 # f
def booklet_order(n):
"""
Provide a list of page numbers for printing a booklet with a duplex
printer.
Sequential arguments:
n -- The total page count. You must provide an even number, so if
the document has an odd number of pages, make it so (makes either
front or back blank--unless you put a blank page before the back
or after the front).
"""
n = int(n)
if n % 2 != 0:
raise ValueError("You must provide an even number of pages."
" Add a blank page if the document does not"
" have an even number, otherwise the necessary"
" page order for printing a booklet will"
" differ.")
ret = []
x = 0 # 0 is left, 1 is right (relative to front or back)
# back = 1 # print on the back twice in a row
p = 1
offset = 2
# for i in range(n//2):
while p <= n:
for back in range(2):
for x in range(2):
if back == 0:
if x == 0:
ret.append(n - (offset - 2))
else:
ret.append(offset - 1)
else:
if x == 0:
ret.append(offset)
else:
ret.append(n - (offset - 1))
p += 1
offset += 2
if n % 4 != 0:
print(file=sys.stderr)
print(
"WARNING: You'll have to cut the page printed last in"
" half, throw away the incorrect half, then flip it"
" properly since page counts not divisible by 4 are not"
" fully implemented.",
file=sys.stderr)
return ret
def main():
if len(sys.argv) < 2: # 0 is script
print(
"You must provide a page count to create booklet"
" page order.",
file=sys.stderr)
exit(1)
print(booklet_order(sys.argv[1]))
pass
if __name__ == "__main__":
main()