Last active
May 2, 2018 14:35
-
-
Save MircoT/b6cff1445450238ba99d4662d7c3a4cf to your computer and use it in GitHub Desktop.
Merge all pdf files in the same folder of this script, using PyPDF2
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
| #!/usr/bin/env python | |
| """Simple script to merge all pdf in the folder. | |
| To do: | |
| 0. Add argparse for input from command line | |
| 1. Merge of all file in a specific folder | |
| 2. Add exclude file list | |
| 3. Add order file list, to custom the merge order | |
| """ | |
| from os import path, walk | |
| from time import sleep | |
| from PyPDF2 import PdfFileMerger, PdfFileReader | |
| OUTPUT_NAME = "all_files.pdf" | |
| def main(): | |
| merger = PdfFileMerger() | |
| descriptors = [] | |
| for root, _, files in walk("."): | |
| for file_ in sorted(files): | |
| if path.splitext(file_)[1] == ".pdf" and file_ != OUTPUT_NAME: | |
| print("==> Add: {}".format(file_)) | |
| cur_pdf = open(path.join(root, file_), 'rb') | |
| descriptors.append(cur_pdf) | |
| merger.append(cur_pdf) | |
| print("==> Write output pdf") | |
| merger.write(OUTPUT_NAME) | |
| sleep(2) | |
| print("==> Close merger") | |
| merger.close() | |
| print("==> Close descriptors") | |
| for descriptor in descriptors: | |
| descriptor.close() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment