Merging pdf files in Python with pyPDF

Here is a very simple python script that marges two pdf files, using the pyPDF library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import os.path
import pyPdf
 
pdfOne = "C:\\a.pdf"
pdfTwo = "C:\\b.pdf"
 
merged = "C:\\c.pdf"
 
if os.path.exists(pdfOne) and os.path.exists(pdfTwo): 
 
	output = pyPdf.PdfFileWriter()
 
	pdfOne = pyPdf.PdfFileReader(open(pdfOne, "rb"))
	for page in range(pdfOne.getNumPages()):
		output.addPage(pdfOne.getPage(page))
 
	pdfTwo = pyPdf.PdfFileReader(open(pdfTwo, "rb"))
	for page in range(pdfTwo.getNumPages()):
		output.addPage(pdfTwo.getPage(page))
 
	outputStream = open(merged, "wb")
	output.write(outputStream)
	outputStream.close()