Skip to content

Instantly share code, notes, and snippets.

@jsundram
Created June 12, 2013 05:37
Show Gist options
  • Select an option

  • Save jsundram/5763042 to your computer and use it in GitHub Desktop.

Select an option

Save jsundram/5763042 to your computer and use it in GitHub Desktop.
Takes a list of pdf filenames and and output filename and merges all the input filenames into one pdf (in the order they were specified).
from glob import glob
import os
from pyPdf import PdfFileWriter, PdfFileReader
def merge_pdfs(pdfs, output_file, delete_originals=True):
""" Takes a list of pdf filenames and and output filename
and merges all the input filenames into one pdf (in
the order they were specified
"""
merged = PdfFileWriter()
for pdf in pdfs:
if os.path.exists(pdf):
r = PdfFileReader(open(pdf, 'rb'))
for page in range(r.getNumPages()):
merged.addPage(r.getPage(page))
with open(output_file, 'wb') as f:
merged.write(f)
if delete_originals:
for pdf in pdfs:
if os.path.exists(pdf):
os.unlink(pdf)
def main():
pdfs = glob("*.pdf")
merge_pdfs(pdfs, "merged.pdf", False)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment