rpb
rpb

Reputation: 3299

Confusion in using argparse in Python

I would like to execute a code which is accessible from this link. The code objective is to read and extract the pdf annotation.

However, I am not sure how to direct the pdf file path using the argparse, which I suspect, should be the following argparse.

p.add_argument("input", metavar="INFILE", type=argparse.FileType("rb"),
                   help="PDF files to process", nargs='+')

Say, I know the absolute path of the pdf file is as follow;

C:\abc.pdf

Also, given that I still try to comprehend the code, so I would like to avoid re-enter the path C:\abc.pdf over and over again. Is there are way, I can temporary hard coded it within the def parse_args()

I have read several thread about this topic, however, still having difficulties in comprehend about this issue.

Thanks in advance for any insight.

Upvotes: 1

Views: 333

Answers (2)

user5386938
user5386938

Reputation:

If you're running pdfannots.py from the command prompt, then you do so as, e.g.,

python pdfannots.py C:\abc.pdf

If you want to run it over multiple PDF files, then you do so as, e.g.,

python pdfannots.py C:\abc.pdf D:\xyz.pdf E:\foo.pdf

If you really want to hard code the path, you'll have to edit pdfannots.py as follows:


def parse_args():
    p = argparse.ArgumentParser(description=__doc__)

    # p.add_argument("input", metavar="INFILE", type=argparse.FileType("rb"),
                   # help="PDF files to process", nargs='+')

    g = p.add_argument_group('Basic options')
    g.add_argument("-p", "--progress", default=False, action="store_true",
                   help="emit progress information")
    g.add_argument("-o", metavar="OUTFILE", type=argparse.FileType("w"), dest="output",
                   default=sys.stdout, help="output file (default is stdout)")
    g.add_argument("-n", "--cols", default=2, type=int, metavar="COLS", dest="cols",
                   help="number of columns per page in the document (default: 2)")

    g = p.add_argument_group('Options controlling output format')
    allsects = ["highlights", "comments", "nits"]
    g.add_argument("-s", "--sections", metavar="SEC", nargs="*",
                   choices=allsects, default=allsects,
                   help=("sections to emit (default: %s)" % ', '.join(allsects)))
    g.add_argument("--no-group", dest="group", default=True, action="store_false",
                   help="emit annotations in order, don't group into sections")
    g.add_argument("--print-filename", dest="printfilename", default=False, action="store_true",
                   help="print the filename when it has annotations")
    g.add_argument("-w", "--wrap", metavar="COLS", type=int,
                   help="wrap text at this many output columns")

    return p.parse_args()


def main():
    args = parse_args()

    global COLUMNS_PER_PAGE
    COLUMNS_PER_PAGE = args.cols

    for file in [open(r"C:\Users\jezequiel\Desktop\Timeline.pdf", "rb")]:
        (annots, outlines) = process_file(file, args.progress)

        pp = PrettyPrinter(outlines, args.wrap)

        if args.printfilename and annots:
            print("# File: '%s'\n" % file.name)

        if args.group:
            pp.printall_grouped(args.sections, annots, args.output)
        else:
            pp.printall(annots, args.output)

    return 0

Upvotes: 2

chepner
chepner

Reputation: 530970

You add the argument to a parser:

import argparse

p = argparse.ArgumentParser()
p.add_argument("input", metavar="INFILE", type=argparse.FileType("rb"),
               help="PDF files to process", nargs='+')
args = p.parse_args()

Then args.input will be a tuple of one or more open file handles.

Upvotes: 3

Related Questions