-
Notifications
You must be signed in to change notification settings - Fork 0
/
manualprintPDF.py
executable file
·66 lines (53 loc) · 1.81 KB
/
manualprintPDF.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
import sys
from pdfrw import PdfReader, PdfWriter, PdfName, PdfDict
"""
usage: python manualprintPDF.py input.py output.py
Creates: output_odd.py and output_even.pdf
"""
class manualPdfPrint:
"""for print the pdf file by manual
"""
def __init__(self, inp_f:str, out_f:str):
self.inp_f = PdfReader(inp_f)
self.gen_out_f(out_f)
self.odd_pages = []
self.even_pages = []
def gen_out_f(self, out_f:str):
out_f = out_f.split('.pdf')[0]
self.out_odd_f = PdfWriter(out_f + '_odd.pdf')
self.out_even_f = PdfWriter(out_f + '_even.pdf')
def check_and_make_even_pages(self):
def blankPage(template):
x = PdfDict()
x.Type = PdfName.Page
x.Contents = PdfDict(stream="")
x.MediaBox = template.inheritable.MediaBox
return x
self.pages = self.inp_f.pages
if len(self.pages) % 2 == 1:
self.pages.append(blankPage(self.pages[0]))
def split_pages(self):
for i, page in enumerate(self.pages):
if i % 2 == 0: # extract odd pages
self.odd_pages.append(page)
else:
self.even_pages.append(page)
# reverse the even pages for printing easier
self.even_pages.reverse()
def save_pdfFile(self):
for i in range(len(self.odd_pages)):
self.out_odd_f.addPage(self.odd_pages[i])
self.out_even_f.addPage(self.even_pages[i])
self.out_odd_f.write()
self.out_even_f.write()
def __call__(self, *args, **kwds):
self.check_and_make_even_pages()
self.split_pages()
self.save_pdfFile()
def main():
inp_f = sys.argv[-2]
outp_f = sys.argv[-1]
printPDF = manualPdfPrint(inp_f, outp_f)
printPDF()
if __name__ == '__main__':
main()