Skip to content

Instantly share code, notes, and snippets.

@absolutelyNoWarranty
Created July 4, 2013 08:37
Show Gist options
  • Save absolutelyNoWarranty/5925982 to your computer and use it in GitHub Desktop.
Save absolutelyNoWarranty/5925982 to your computer and use it in GitHub Desktop.
Join a bunch of pdfs (like chapters of a book) into one pdf.
from pyPdf import PdfFileReader, PdfFileWriter
import sys, os
if len(sys.argv)>1:
output_name = sys.argv[1]
else:
output_name = "all.pdf"
pdfs = filter(lambda f: os.path.splitext(f)[1]=='.pdf', os.walk(".").next()[2])
pdfs.sort()
print pdfs
# join them all together
output = PdfFileWriter()
for fname in pdfs:
pdf = PdfFileReader(file(fname, "rb"))
for page in range(pdf.getNumPages()):
output.addPage(pdf.getPage(page))
output.write(file(output_name, "wb"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment