cli.py 3 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.data.data_reader_writer import FileBasedDataReader
9
from magic_pdf.libs.version import __version__
10
from magic_pdf.tools.common import do_parse, parse_pdf_methods
icecraft's avatar
icecraft committed
11
12
13


@click.command()
14
15
16
17
@click.version_option(__version__,
                      '--version',
                      '-v',
                      help='display the version and exit')
icecraft's avatar
icecraft committed
18
@click.option(
19
20
21
    '-p',
    '--path',
    'path',
icecraft's avatar
icecraft committed
22
23
    type=click.Path(exists=True),
    required=True,
24
    help='local pdf filepath or directory',
icecraft's avatar
icecraft committed
25
26
)
@click.option(
27
28
29
30
31
32
    '-o',
    '--output-dir',
    'output_dir',
    type=click.Path(),
    required=True,
    help='output local directory',
icecraft's avatar
icecraft committed
33
34
)
@click.option(
35
36
37
    '-m',
    '--method',
    'method',
icecraft's avatar
icecraft committed
38
    type=parse_pdf_methods,
39
    help="""the method for parsing pdf.
icecraft's avatar
icecraft committed
40
ocr: using ocr technique to extract information from pdf.
41
42
43
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.""",
44
    default='auto',
icecraft's avatar
icecraft committed
45
)
46
47
48
49
50
51
52
53
@click.option(
    '-l',
    '--lang',
    'lang',
    type=str,
    help="""
    Input the languages in the pdf (if known) to improve OCR accuracy.  Optional.
    You should input "Abbreviation" with language form url:
54
    https://paddlepaddle.github.io/PaddleOCR/latest/en/ppocr/blog/multi_languages.html#5-support-languages-and-abbreviations
55
56
57
    """,
    default=None,
)
58
@click.option(
59
60
61
    '-d',
    '--debug',
    'debug_able',
62
    type=bool,
63
    help='Enables detailed debugging information during the execution of the CLI commands.',
64
65
    default=False,
)
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
@click.option(
    '-s',
    '--start',
    'start_page_id',
    type=int,
    help='The starting page for PDF parsing, beginning from 0.',
    default=0,
)
@click.option(
    '-e',
    '--end',
    'end_page_id',
    type=int,
    help='The ending page for PDF parsing, beginning from 0.',
    default=None,
)
82
def cli(path, output_dir, method, lang, debug_able, start_page_id, end_page_id):
icecraft's avatar
icecraft committed
83
    model_config.__use_inside_model__ = True
84
85
    model_config.__model_mode__ = 'full'
    os.makedirs(output_dir, exist_ok=True)
icecraft's avatar
icecraft committed
86
87

    def read_fn(path):
88
89
        disk_rw = FileBasedDataReader(os.path.dirname(path))
        return disk_rw.read(os.path.basename(path))
icecraft's avatar
icecraft committed
90
91
92
93
94
95
96
97
98
99
100

    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,
101
                debug_able,
102
103
                start_page_id=start_page_id,
                end_page_id=end_page_id,
104
                lang=lang
icecraft's avatar
icecraft committed
105
106
107
108
109
110
            )

        except Exception as e:
            logger.exception(e)

    if os.path.isdir(path):
111
        for doc_path in Path(path).glob('*.pdf'):
icecraft's avatar
icecraft committed
112
113
114
115
116
            parse_doc(doc_path)
    else:
        parse_doc(path)


117
if __name__ == '__main__':
icecraft's avatar
icecraft committed
118
    cli()