cli.py 2.27 KB
Newer Older
icecraft's avatar
icecraft committed
1
import os
2
3
from pathlib import Path

icecraft's avatar
icecraft committed
4
5
import click
from loguru import logger
6

icecraft's avatar
icecraft committed
7
import magic_pdf.model as model_config
8
from magic_pdf.libs.version import __version__
9
10
11
from magic_pdf.rw.AbsReaderWriter import AbsReaderWriter
from magic_pdf.rw.DiskReaderWriter import DiskReaderWriter
from magic_pdf.tools.common import do_parse, parse_pdf_methods
icecraft's avatar
icecraft committed
12
13
14


@click.command()
15
16
17
18
@click.version_option(__version__,
                      '--version',
                      '-v',
                      help='display the version and exit')
icecraft's avatar
icecraft committed
19
@click.option(
20
21
22
    '-p',
    '--path',
    'path',
icecraft's avatar
icecraft committed
23
24
    type=click.Path(exists=True),
    required=True,
25
    help='local pdf filepath or directory',
icecraft's avatar
icecraft committed
26
27
)
@click.option(
28
29
30
31
32
33
34
    '-o',
    '--output-dir',
    'output_dir',
    type=click.Path(),
    required=True,
    help='output local directory',
    default='',
icecraft's avatar
icecraft committed
35
36
)
@click.option(
37
38
39
    '-m',
    '--method',
    'method',
icecraft's avatar
icecraft committed
40
    type=parse_pdf_methods,
41
    help="""the method for parsing pdf.
icecraft's avatar
icecraft committed
42
ocr: using ocr technique to extract information from pdf.
43
44
45
txt: suitable for the text-based pdf only and outperform ocr.
auto: automatically choose the best method for parsing pdf from ocr and txt.
without method specified, auto will be used by default.""",
46
    default='auto',
icecraft's avatar
icecraft committed
47
)
48
49
50
51
52
53
54
55
56
@click.option(
    "-d",
    "--debug",
    "debug_able",
    type=bool,
    help="Enables detailed debugging information during the execution of the CLI commands.",
    default=False,
)
def cli(path, output_dir, method, debug_able):
icecraft's avatar
icecraft committed
57
    model_config.__use_inside_model__ = True
58
59
    model_config.__model_mode__ = 'full'
    os.makedirs(output_dir, exist_ok=True)
icecraft's avatar
icecraft committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

    def read_fn(path):
        disk_rw = DiskReaderWriter(os.path.dirname(path))
        return disk_rw.read(os.path.basename(path), AbsReaderWriter.MODE_BIN)

    def parse_doc(doc_path: str):
        try:
            file_name = str(Path(doc_path).stem)
            pdf_data = read_fn(doc_path)
            do_parse(
                output_dir,
                file_name,
                pdf_data,
                [],
                method,
75
                debug_able,
icecraft's avatar
icecraft committed
76
77
78
79
80
81
            )

        except Exception as e:
            logger.exception(e)

    if os.path.isdir(path):
82
        for doc_path in Path(path).glob('*.pdf'):
icecraft's avatar
icecraft committed
83
84
85
86
87
            parse_doc(doc_path)
    else:
        parse_doc(path)


88
if __name__ == '__main__':
icecraft's avatar
icecraft committed
89
    cli()