Created
December 4, 2020 23:06
-
-
Save gtindo/674bde08453b14822d1c7dee49a5ec2e to your computer and use it in GitHub Desktop.
Script for merging all pdf inside a folder as a single pdf
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
""" | |
Script for merging all pdf inside a folder as a single pdf | |
Requirement: PyPDF2 | |
usage: python merge_pdf.py -d <input_directory> -o output.pdf | |
Author: Gtindo Dev (https://gtindo.dev) | |
License: ISC | |
""" | |
from PyPDF2 import PdfFileReader, PdfFileWriter | |
import argparse | |
import os | |
parser = argparse.ArgumentParser(description='Merge multiple pdf files') | |
parser.add_argument('--dir', '-d', help='Directory that contain pdf to join') | |
parser.add_argument('--output', '-o', help="Output pdf name") | |
args = parser.parse_args() | |
def is_pdf(filename): | |
if filename.split(".")[1] == "pdf": | |
return True | |
return False | |
def join_by_directory(dir_path, output_file): | |
pdf_writer = PdfFileWriter() | |
try: | |
directory = os.listdir(dir_path) | |
print("[+] Start Merging files of {}".format(dir_path)) | |
for file in directory: | |
if not is_pdf(file): | |
continue | |
pdf_path = os.path.join(dir_path, file) | |
pdf_reader = PdfFileReader(pdf_path) | |
print("[-] Merging file {}".format(file)) | |
for page in range(pdf_reader.getNumPages()): | |
pdf_writer.addPage(pdf_reader.getPage(page)) | |
with open(output_file, 'wb') as out: | |
pdf_writer.write(out) | |
print("[x] Pdf joined successfully !") | |
except FileNotFoundError: | |
print("[Error] - Directory {} does not exists.".format(dir_path)) | |
exit(0) | |
if __name__ == '__main__': | |
if args.output is None: | |
print("Output file name is missing") | |
exit(0) | |
if args.dir is None: | |
print("Directory path is missing") | |
exit(0) | |
join_by_directory(args.dir, args.output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment