cli.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import os
  2. from pathlib import Path
  3. import click
  4. from loguru import logger
  5. import magic_pdf.model as model_config
  6. from magic_pdf.libs.version import __version__
  7. from magic_pdf.rw.AbsReaderWriter import AbsReaderWriter
  8. from magic_pdf.rw.DiskReaderWriter import DiskReaderWriter
  9. from magic_pdf.tools.common import do_parse, parse_pdf_methods
  10. @click.command()
  11. @click.version_option(__version__,
  12. '--version',
  13. '-v',
  14. help='display the version and exit')
  15. @click.option(
  16. '-p',
  17. '--path',
  18. 'path',
  19. type=click.Path(exists=True),
  20. required=True,
  21. help='local pdf filepath or directory',
  22. )
  23. @click.option(
  24. '-o',
  25. '--output-dir',
  26. 'output_dir',
  27. type=click.Path(),
  28. required=True,
  29. help='output local directory',
  30. )
  31. @click.option(
  32. '-m',
  33. '--method',
  34. 'method',
  35. type=parse_pdf_methods,
  36. help="""the method for parsing pdf.
  37. ocr: using ocr technique to extract information from pdf.
  38. txt: suitable for the text-based pdf only and outperform ocr.
  39. auto: automatically choose the best method for parsing pdf from ocr and txt.
  40. without method specified, auto will be used by default.""",
  41. default='auto',
  42. )
  43. @click.option(
  44. '-l',
  45. '--lang',
  46. 'lang',
  47. type=str,
  48. help="""
  49. Input the languages in the pdf (if known) to improve OCR accuracy. Optional.
  50. You should input "Abbreviation" with language form url:
  51. https://paddlepaddle.github.io/PaddleOCR/en/ppocr/blog/multi_languages.html#5-support-languages-and-abbreviations
  52. """,
  53. default=None,
  54. )
  55. @click.option(
  56. '-d',
  57. '--debug',
  58. 'debug_able',
  59. type=bool,
  60. help='Enables detailed debugging information during the execution of the CLI commands.',
  61. default=False,
  62. )
  63. @click.option(
  64. '-s',
  65. '--start',
  66. 'start_page_id',
  67. type=int,
  68. help='The starting page for PDF parsing, beginning from 0.',
  69. default=0,
  70. )
  71. @click.option(
  72. '-e',
  73. '--end',
  74. 'end_page_id',
  75. type=int,
  76. help='The ending page for PDF parsing, beginning from 0.',
  77. default=None,
  78. )
  79. def cli(path, output_dir, method, lang, debug_able, start_page_id, end_page_id):
  80. model_config.__use_inside_model__ = True
  81. model_config.__model_mode__ = 'full'
  82. os.makedirs(output_dir, exist_ok=True)
  83. def read_fn(path):
  84. disk_rw = DiskReaderWriter(os.path.dirname(path))
  85. return disk_rw.read(os.path.basename(path), AbsReaderWriter.MODE_BIN)
  86. def parse_doc(doc_path: str):
  87. try:
  88. file_name = str(Path(doc_path).stem)
  89. pdf_data = read_fn(doc_path)
  90. do_parse(
  91. output_dir,
  92. file_name,
  93. pdf_data,
  94. [],
  95. method,
  96. debug_able,
  97. start_page_id=start_page_id,
  98. end_page_id=end_page_id,
  99. lang=lang
  100. )
  101. except Exception as e:
  102. logger.exception(e)
  103. if os.path.isdir(path):
  104. for doc_path in Path(path).glob('*.pdf'):
  105. parse_doc(doc_path)
  106. else:
  107. parse_doc(path)
  108. if __name__ == '__main__':
  109. cli()