Created
June 12, 2013 05:37
-
-
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).
This file contains hidden or 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 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