-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathimposition.py
More file actions
executable file
·141 lines (121 loc) · 4.43 KB
/
imposition.py
File metadata and controls
executable file
·141 lines (121 loc) · 4.43 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python3
# usage:
# scribus -g -py imposition.py -- filename.sla
# Creates a pdf at the original size and one imposed on A4.
#
# or
#
# python imposition.py [filename.sla]
# Call this script through scribus for each file in the directory or for the given Scribus file
#
# TODO:
# - if the python script has a .sla as an argument, place the PDFs in that path.
# - check if we can do the same with podofo and create a libui UI around this that works on the pdf generated by Scribus (and does not depend on Scribus)
import os
import sys
from subprocess import call
def main():
try:
import scribus
except ImportError:
# if the script does not run inside of scribus, launch scribus with this script as paremeter
filename = []
if (len(sys.argv) == 2) :
print("launching the script for " + sys.argv[1])
filename.append(sys.argv[1])
else:
print("launching the script for each .sla in the directory.")
for file in os.listdir('.'):
filenameSplit = os.path.splitext(file)
if filenameSplit[-1] == '.sla' and filenameSplit[0].find('_autosave_') == -1:
filename.append(file)
arguments = []
for argument in sys.argv[1:]:
if argument[0] == '-':
# arguments.append('--python-arg')
arguments.append('-pa')
arguments.append(argument.strip('-'))
print(arguments)
for file in filename :
call(['scribus', '-g', '-py', sys.argv[0]] + arguments + ['--', file])
sys.exit(1)
if scribus.haveDoc():
page_sizes = {(210, 297): 'A4', (148, 210): 'A5', (105, 148): 'A6'}
filename = os.path.splitext(scribus.getDocName())[0]
pdf = scribus.PDFfile()
page_size = tuple(int(round(s)) for s in scribus.getPageSize())
print(page_sizes[page_size])
page_size = page_sizes[page_size]
pdf.file = filename + '-' + page_size.lower() + '.pdf'
pdf.save()
single_sided = 'single' in sys.argv
print(sys.argv)
print(single_sided)
# sys.exit(1)
pdf = scribus.PDFfile()
pdf.file = filename + "-reordered.pdf"
pdf.pages = get_a4_pages(page_size, single_sided, scribus.pageCount())
print(page_size)
print(single_sided)
print(scribus.pageCount())
print(pdf.pages)
pdf.save()
# os.system("pdfnup --nup 2x2 --frame false --no-landscape " + filename + "-reordered.pdf --outfile " + filename.replace("a6", "a4") + ".pdf")
call(['pdfjam', '--nup', '2x2', '--frame', 'false', '--no-landscape', filename + '-reordered.pdf', '--outfile', filename + "-a4.pdf"])
os.remove(pdf.file)
else:
print("No file open");
def get_a4_pages(size, single, pages_count):
if size == 'A5':
return get_a4_pages_from_a5(single, pages_count)
elif size == 'A6':
return get_a4_pages_from_a6(single, pages_count)
else:
print(f'{scribus.pageCount()} are not yet supported')
return []
def get_a4_pages_from_a5(single, pages_count):
if single:
if pages_count > 2:
pages = range(1, pages_count + 1)
else:
pages_layout = {
1: [1, 1],
2: [1, 2],
}
pages = pages_layout[pages_count]
else:
pages_layout = {
1: [1, 1, 1, 1],
2: [1, 1, 2, 2],
4: [0], # just use pdfbook
8: [0], # just use pdfbook
12: [0], # just use pdfbook
16: [0], # just use pdfbook
}
pages = pages_layout[pages_count]
return pages
def get_a4_pages_from_a6(single, pages_count):
if single:
pages_layout = {
1: [1, 1, 1, 1],
2: [1, 1, 2, 2],
4: range(1, 5),
8: range(1, 9),
12: range(1, 13),
16: range(1, 17)
}
pages = pages_layout[pages_count]
else:
pages_layout = {
1: [1, 1, 1, 1],
2: [1, 1, 1, 1, 2, 2, 2, 2],
4: [4, 1, 4, 1, 2, 3, 2, 3],
8: [8, 1, 6, 3, 2, 7, 4, 5],
12: [12, 1, 2, 11, 10, 3, 4, 9, 8, 5, 6, 7],
# <- first join top/down , then staple 1/2
16: [16, 1, 14 , 3, 2, 15, 4, 13, 12, 5, 10, 7, 6, 11, 8, 9]
}
pages = pages_layout[pages_count]
return pages
if __name__ == "__main__":
main()