-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.py
74 lines (55 loc) · 2.14 KB
/
merge.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
#!/usr/bin/python
import PyPDF2
import sys
import os
import time
def merge(input_files, output_file):
"""
merges given input pdf files into single pdf file
"""
output = PyPDF2.PdfFileWriter()
for input_file in input_files:
input = PyPDF2.PdfFileReader( open(input_file, "rb"), strict=False )
rotate = 0
if input_file.lower().endswith('-rotate-90.pdf'):
rotate = 90
elif input_file.lower().endswith('-rotate-180.pdf'):
rotate = 180
elif input_file.lower().endswith('-rotate-270.pdf'):
rotate = 270
for i in range(input.numPages):
page = input.getPage(i)
if rotate > 0:
page = page.rotate(rotate)
output.addPage(page)
with open(output_file, "wb") as outputStream:
output.write(outputStream)
return True
def main(args):
# if there is no argument provided, find pdfs automatically from input/ dir
if len(args) == 1:
root_dir = os.path.dirname(__file__)
input_dir = os.path.join(root_dir, 'input')
if os.path.isdir(input_dir):
pdfs = [f for f in os.listdir(input_dir) if f.lower().endswith('.pdf')]
args.extend([os.path.join(input_dir, f) for f in pdfs])
output_filename = "output-%s.pdf" % int(time.time())
args.append(os.path.join(root_dir, output_filename))
# at least 3 arguments are needed: program name itself, input, output
if len(args) < 3:
print("Usage: Put all PDFs inside \"input/\" directory.")
print(" If you want to rotate pages of a PDF file, append \"-rotate-{angle}\" to file name.")
print(" Ex: scan-rotate-180.pdf will be rotated 180 degrees before merge")
print("")
print("Command line usage: %s input1... output" % args[0])
return
input_files = args[1:-1]
output_file = args[-1]
if merge(input_files, output_file):
print('Merged PDF created: ' + output_file)
if __name__ == "__main__":
try:
main(sys.argv)
except Exception as e:
print(e)
input('\nPress any key to continue')