cli.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. '-d',
  45. '--debug',
  46. 'debug_able',
  47. type=bool,
  48. help='Enables detailed debugging information during the execution of the CLI commands.',
  49. default=False,
  50. )
  51. @click.option(
  52. '-s',
  53. '--start',
  54. 'start_page_id',
  55. type=int,
  56. help='The starting page for PDF parsing, beginning from 0.',
  57. default=0,
  58. )
  59. @click.option(
  60. '-e',
  61. '--end',
  62. 'end_page_id',
  63. type=int,
  64. help='The ending page for PDF parsing, beginning from 0.',
  65. default=None,
  66. )
  67. def cli(path, output_dir, method, debug_able, start_page_id, end_page_id):
  68. model_config.__use_inside_model__ = True
  69. model_config.__model_mode__ = 'full'
  70. os.makedirs(output_dir, exist_ok=True)
  71. def read_fn(path):
  72. disk_rw = DiskReaderWriter(os.path.dirname(path))
  73. return disk_rw.read(os.path.basename(path), AbsReaderWriter.MODE_BIN)
  74. def parse_doc(doc_path: str):
  75. try:
  76. file_name = str(Path(doc_path).stem)
  77. pdf_data = read_fn(doc_path)
  78. do_parse(
  79. output_dir,
  80. file_name,
  81. pdf_data,
  82. [],
  83. method,
  84. debug_able,
  85. start_page_id=start_page_id,
  86. end_page_id=end_page_id,
  87. )
  88. except Exception as e:
  89. logger.exception(e)
  90. if os.path.isdir(path):
  91. for doc_path in Path(path).glob('*.pdf'):
  92. parse_doc(doc_path)
  93. else:
  94. parse_doc(path)
  95. if __name__ == '__main__':
  96. cli()