Created
February 23, 2019 17:31
-
-
Save Alquimista/cadc824da944e9e2eccb1ed5d535d8db to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*-s | |
from pyPdf import PdfFileWriter, PdfFileReader | |
from optparse import OptionParser | |
def mergepdf(outputfile, filestobemerged): | |
output = PdfFileWriter() | |
for infile in filestobemerged: | |
# Read the input PDF file | |
input = PdfFileReader(open(infile, "rb")) | |
# Add every page in input PDF file to output | |
for page in input.pages: | |
output.addPage(page) | |
# Output stream for output file | |
with open(outputfile, "wb") as outputstream: | |
output.write(outputstream) | |
def main(): | |
parser = OptionParser( | |
usage="%prog [options] FILE...", | |
description="Merges PDF documents into one.") | |
parser.add_option( | |
"-o", | |
"--output", | |
dest="output_name", | |
default="out.pdf", | |
help="Specify output file name [default: %default]") | |
options, args = parser.parse_args() | |
if len(args) < 2: | |
parser.error("Incorrect number of arguments") | |
else: | |
mergepdf(options.output_name, args) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment