def cat(args): """ %prog cat *.pdf -o output.pdf Concatenate pages from pdf files into a single pdf file. Page ranges refer to the previously-named file. A file not followed by a page range means all the pages of the file. PAGE RANGES are like Python slices. {page_range_help} EXAMPLES pdfcat -o output.pdf head.pdf content.pdf :6 7: tail.pdf -1 Concatenate all of head.pdf, all but page seven of content.pdf, and the last page of tail.pdf, producing output.pdf. pdfcat chapter*.pdf >book.pdf You can specify the output file by redirection. pdfcat chapter?.pdf chapter10.pdf >book.pdf In case you don't want chapter 10 before chapter 2. """ p = OptionParser(cat.__doc__.format(page_range_help=PAGE_RANGE_HELP)) p.add_option("--nosort", default=False, action="store_true", help="Do not sort file names") p.set_outfile() p.set_verbose(help="Show page ranges as they are being read") opts, args = p.parse_args(args) if len(args) < 1: sys.exit(not p.print_help()) outfile = opts.outfile if outfile in args: args.remove(outfile) if not opts.nosort: args = natsorted(args) filename_page_ranges = parse_filename_page_ranges(args) verbose = opts.verbose fw = must_open(outfile, "wb") merger = PdfFileMerger() in_fs = {} try: for (filename, page_range) in filename_page_ranges: if verbose: print >> sys.stderr, filename, page_range if filename not in in_fs: in_fs[filename] = open(filename, "rb") merger.append(in_fs[filename], pages=page_range) except: print >> sys.stderr, traceback.format_exc() print >> sys.stderr, "Error while reading " + filename sys.exit(1) merger.write(fw) fw.close()
def cat(args): """ %prog cat *.pdf -o output.pdf Concatenate pages from pdf files into a single pdf file. Page ranges refer to the previously-named file. A file not followed by a page range means all the pages of the file. PAGE RANGES are like Python slices. {page_range_help} EXAMPLES pdfcat -o output.pdf head.pdf content.pdf :6 7: tail.pdf -1 Concatenate all of head.pdf, all but page seven of content.pdf, and the last page of tail.pdf, producing output.pdf. pdfcat chapter*.pdf >book.pdf You can specify the output file by redirection. pdfcat chapter?.pdf chapter10.pdf >book.pdf In case you don't want chapter 10 before chapter 2. """ p = OptionParser(cat.__doc__.format(page_range_help=PAGE_RANGE_HELP)) p.set_outfile() p.set_verbose(help="Show page ranges as they are being read") opts, args = p.parse_args(args) if len(args) < 1: sys.exit(not p.print_help()) outfile = opts.outfile if outfile in args: args.remove(outfile) args = natsorted(args) filename_page_ranges = parse_filename_page_ranges(args) verbose = opts.verbose fw = must_open(outfile, "wb") merger = PdfFileMerger() in_fs = {} try: for (filename, page_range) in filename_page_ranges: if verbose: print >> sys.stderr, filename, page_range if filename not in in_fs: in_fs[filename] = open(filename, "rb") merger.append(in_fs[filename], pages=page_range) except: print >> sys.stderr, traceback.format_exc() print >> sys.stderr, "Error while reading " + filename sys.exit(1) merger.write(fw) fw.close()
args = parser.parse_args() args.fn_pgrgs.insert(0, args.first_filename[0]) return args from sys import stderr, stdout, exit import os import traceback from collections import defaultdict from PyPDF2 import PdfFileMerger, parse_filename_page_ranges if __name__ == "__main__": args = parse_args() filename_page_ranges = parse_filename_page_ranges(args.fn_pgrgs) if args.output: output = open(args.output, "wb") else: stdout.flush() output = os.fdopen(stdout.fileno(), "wb") merger = PdfFileMerger() in_fs = dict() try: for (filename, page_range) in filename_page_ranges: if args.verbose: print(filename, page_range, file=stderr) if filename not in in_fs: in_fs[filename] = open(filename, "rb") merger.append(in_fs[filename], pages=page_range)