-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergePDF.py
executable file
·30 lines (24 loc) · 947 Bytes
/
mergePDF.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
#!/usr/bin/env python3
import PyPDF2
import sys
import os.path as op
if __name__=='__main__':
pdfWriter = PyPDF2.PdfFileWriter()
argc=len(sys.argv)
if argc<4:
print('Usage:\nmergePDF.py output.pdf input1.pdf input2.pdf ...')
sys.exit(1)
for fpdf in sys.argv[2:argc]:
if not op.isfile(fpdf):
print(fpdf,' does not exist... skipping')
continue
f = open(fpdf, 'rb')
print('-->',fpdf)
pdfReader = PyPDF2.PdfFileReader(f)
print('pages:',pdfReader.numPages)
for pageNum in range(pdfReader.numPages):
pageObj = pdfReader.getPage(pageNum)
pdfWriter.addPage(pageObj)
pdfOutputFile = open(sys.argv[1], 'wb')
pdfWriter.write(pdfOutputFile)
pdfOutputFile.close()