Last active
August 7, 2017 02:24
-
-
Save monkut/5429be2538e3fbf74f30da0faad1a648 to your computer and use it in GitHub Desktop.
Merge Multiple PDFs into 1 with PyPDF2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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