Skip to content

Instantly share code, notes, and snippets.

@monkut
Last active August 7, 2017 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkut/5429be2538e3fbf74f30da0faad1a648 to your computer and use it in GitHub Desktop.
Save monkut/5429be2538e3fbf74f30da0faad1a648 to your computer and use it in GitHub Desktop.
Merge Multiple PDFs into 1 with PyPDF2
from PyPDF2 import PdfFileReader, PdfFileMerger
pdfs_to_merge = [
# Page numbers are 0 start
# (FILEPATH, (START_PAGE, UNTIL_PAGE)) -- If None, all pages output
(None, (0, 5),
]
# Creating an object where pdf pages are appended to
merged_output = PdfFileMerger()
for pdf_filepath, pages in pdfs_to_merge:
if pages:
merged_output.append(pdf_filepath, pages=pages)
else:
merged_output.append(pdf_filepath)
with open("OUTPUT.pdf","wb") as out_pdf:
merged_output.write(out_pdf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment