to_markdown.rst 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. 转换为 Markdown 文件
  2. ========================
  3. 本地文件示例
  4. ^^^^^^^^^^^^^^^^^^
  5. .. code:: python
  6. import os
  7. from magic_pdf.data.data_reader_writer import FileBasedDataWriter, FileBasedDataReader
  8. from magic_pdf.data.dataset import PymuDocDataset
  9. from magic_pdf.model.doc_analyze_by_custom_model import doc_analyze
  10. from magic_pdf.config.enums import SupportedPdfParseMethod
  11. # args
  12. pdf_file_name = "abc.pdf" # replace with the real pdf path
  13. name_without_suff = pdf_file_name.split(".")[0]
  14. # prepare env
  15. local_image_dir, local_md_dir = "output/images", "output"
  16. image_dir = str(os.path.basename(local_image_dir))
  17. os.makedirs(local_image_dir, exist_ok=True)
  18. image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(
  19. local_md_dir
  20. )
  21. image_dir = str(os.path.basename(local_image_dir))
  22. # read bytes
  23. reader1 = FileBasedDataReader("")
  24. pdf_bytes = reader1.read(pdf_file_name) # read the pdf content
  25. # proc
  26. ## Create Dataset Instance
  27. ds = PymuDocDataset(pdf_bytes)
  28. ## inference
  29. if ds.classify() == SupportedPdfParseMethod.OCR:
  30. infer_result = ds.apply(doc_analyze, ocr=True)
  31. ## pipeline
  32. pipe_result = infer_result.pipe_ocr_mode(image_writer)
  33. else:
  34. infer_result = ds.apply(doc_analyze, ocr=False)
  35. ## pipeline
  36. pipe_result = infer_result.pipe_txt_mode(image_writer)
  37. ### draw model result on each page
  38. infer_result.draw_model(os.path.join(local_md_dir, f"{name_without_suff}_model.pdf"))
  39. ### draw layout result on each page
  40. pipe_result.draw_layout(os.path.join(local_md_dir, f"{name_without_suff}_layout.pdf"))
  41. ### draw spans result on each page
  42. pipe_result.draw_span(os.path.join(local_md_dir, f"{name_without_suff}_spans.pdf"))
  43. ### dump markdown
  44. pipe_result.dump_md(md_writer, f"{name_without_suff}.md", image_dir)
  45. ### dump content list
  46. pipe_result.dump_content_list(md_writer, f"{name_without_suff}_content_list.json", image_dir)
  47. 对象存储文件示例
  48. ^^^^^^^^^^^^^^^^
  49. .. code:: python
  50. import os
  51. from magic_pdf.data.data_reader_writer import S3DataReader, S3DataWriter
  52. from magic_pdf.data.dataset import PymuDocDataset
  53. from magic_pdf.model.doc_analyze_by_custom_model import doc_analyze
  54. bucket_name = "{Your S3 Bucket Name}" # replace with real bucket name
  55. ak = "{Your S3 access key}" # replace with real s3 access key
  56. sk = "{Your S3 secret key}" # replace with real s3 secret key
  57. endpoint_url = "{Your S3 endpoint_url}" # replace with real s3 endpoint_url
  58. reader = S3DataReader('unittest/tmp/', bucket_name, ak, sk, endpoint_url) # replace `unittest/tmp` with the real s3 prefix
  59. writer = S3DataWriter('unittest/tmp', bucket_name, ak, sk, endpoint_url)
  60. image_writer = S3DataWriter('unittest/tmp/images', bucket_name, ak, sk, endpoint_url)
  61. # args
  62. pdf_file_name = (
  63. "s3://llm-pdf-text-1/unittest/tmp/bug5-11.pdf" # replace with the real s3 path
  64. )
  65. # prepare env
  66. local_dir = "output"
  67. name_without_suff = os.path.basename(pdf_file_name).split(".")[0]
  68. # read bytes
  69. pdf_bytes = reader.read(pdf_file_name) # read the pdf content
  70. # proc
  71. ## Create Dataset Instance
  72. ds = PymuDocDataset(pdf_bytes)
  73. ## inference
  74. if ds.classify() == SupportedPdfParseMethod.OCR:
  75. infer_result = ds.apply(doc_analyze, ocr=True)
  76. ## pipeline
  77. pipe_result = infer_result.pipe_ocr_mode(image_writer)
  78. else:
  79. infer_result = ds.apply(doc_analyze, ocr=False)
  80. ## pipeline
  81. pipe_result = infer_result.pipe_txt_mode(image_writer)
  82. ### draw model result on each page
  83. infer_result.draw_model(os.path.join(local_dir, f'{name_without_suff}_model.pdf')) # dump to local
  84. ### draw layout result on each page
  85. pipe_result.draw_layout(os.path.join(local_dir, f'{name_without_suff}_layout.pdf')) # dump to local
  86. ### draw spans result on each page
  87. pipe_result.draw_span(os.path.join(local_dir, f'{name_without_suff}_spans.pdf')) # dump to local
  88. ### dump markdown
  89. pipe_result.dump_md(writer, f'{name_without_suff}.md', "unittest/tmp/images") # dump to remote s3
  90. ### dump content list
  91. pipe_result.dump_content_list(md_writer, f"{name_without_suff}_content_list.json", image_dir)
  92. 前去 :doc:`../data/data_reader_writer` 获取更多有关 **读写** 示例