Skip to content

Instantly share code, notes, and snippets.

@Alquimista
Created February 23, 2019 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alquimista/cadc824da944e9e2eccb1ed5d535d8db to your computer and use it in GitHub Desktop.
Save Alquimista/cadc824da944e9e2eccb1ed5d535d8db to your computer and use it in GitHub Desktop.
#!/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