Created
May 4, 2020 11:35
-
-
Save ozencb/78c71c8c67a99e2e4bd96ee673e37f9c to your computer and use it in GitHub Desktop.
Python gist for merging / concatenating PDFs
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
""" | |
PDF Merger | |
Args: -o or --output for output name | |
-p or --path for output path | |
""" | |
import os | |
from argparse import ArgumentParser | |
from glob import glob | |
from pdfrw import PdfReader, PdfWriter, IndirectPdfDict | |
def merge(path, output_filename): | |
writer = PdfWriter() | |
pdfs = [] | |
for pdffile in glob(path + os.sep + '*.pdf'): | |
if pdffile == output_filename: | |
continue | |
print("Listing '%s'" % pdffile) | |
pdfs.append(pdffile) | |
for pdf in pdfs: | |
print("Parsing '%s'" % pdf) | |
writer.addpages(PdfReader(pdf).pages) | |
print("Start writing '%s'" % output_filename) | |
writer.write("Merged PDF.pdf") | |
if __name__ == "__main__": | |
parser = ArgumentParser() | |
parser.add_argument("-o", "--output", | |
dest="output_filename", | |
default="MergedPDF.pdf", | |
help="write merged PDF to FILE", | |
metavar="FILE") | |
parser.add_argument("-p", "--path", | |
dest="path", | |
default=".", | |
help="path of source PDF files") | |
args = parser.parse_args() | |
merge(args.path, args.output_filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is best python script I've ever seen b4. <3