Skip to content

Instantly share code, notes, and snippets.

@amar-sanakal
Last active August 29, 2015 14:21
Show Gist options
  • Save amar-sanakal/bc065c6748ef4723b36b to your computer and use it in GitHub Desktop.
Save amar-sanakal/bc065c6748ef4723b36b to your computer and use it in GitHub Desktop.
Merge multiple PDF files into one file
import os
import sys
from PyPDF2 import PdfFileReader, PdfFileMerger
files_dir = os.path.expanduser("~/pdf")
if not os.path.isdir(files_dir):
print("ERROR: input directory {} does not exist\n".format(files_dir))
sys.exit(1)
pdf_files = [f for f in os.listdir(files_dir) if f.endswith("pdf")]
print("found {} pdfs\n".format(len(pdf_files)))
if len(pdf_files) == 0:
print("ERROR: no PDF files found in {}".format(files_dir))
sys.exit(1)
merger = PdfFileMerger()
for filename in pdf_files:
merger.append(PdfFileReader(os.path.join(files_dir, filename), "rb"))
merger.write(os.path.join(files_dir, "merged_full.pdf"))
@amar-sanakal
Copy link
Author

I found this code at http://www.boxcontrol.net/merge-pdf-files-with-under-10-lines-in-python.html and added it here with some minor modifications. I'm planning to develop it further but for now I like it saved and handy.

@amar-sanakal
Copy link
Author

I've tested this code with Python 3.4.3 on Windows but expect it to work on any *nix platform as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment