to_markdown.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. Convert To Markdown
  2. ========================
  3. Local File Example
  4. ^^^^^^^^^^^^^^^^^^
  5. .. code:: python
  6. import os
  7. from magic_pdf.data.data_reader_writer import FileBasedDataWriter, FileBasedDataReader
  8. from magic_pdf.config.make_content_config import DropMode, MakeMode
  9. from magic_pdf.pipe.OCRPipe import OCRPipe
  10. ## args
  11. model_list = []
  12. pdf_file_name = "abc.pdf" # replace with the real pdf path
  13. ## prepare env
  14. local_image_dir, local_md_dir = "output/images", "output"
  15. os.makedirs(local_image_dir, exist_ok=True)
  16. image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(
  17. local_md_dir
  18. )
  19. image_dir = str(os.path.basename(local_image_dir))
  20. reader1 = FileBasedDataReader("")
  21. pdf_bytes = reader1.read(pdf_file_name) # read the pdf content
  22. pipe = OCRPipe(pdf_bytes, model_list, image_writer)
  23. pipe.pipe_classify()
  24. pipe.pipe_analyze()
  25. pipe.pipe_parse()
  26. pdf_info = pipe.pdf_mid_data["pdf_info"]
  27. md_content = pipe.pipe_mk_markdown(
  28. image_dir, drop_mode=DropMode.NONE, md_make_mode=MakeMode.MM_MD
  29. )
  30. if isinstance(md_content, list):
  31. md_writer.write_string(f"{pdf_file_name}.md", "\n".join(md_content))
  32. else:
  33. md_writer.write_string(f"{pdf_file_name}.md", md_content)
  34. S3 File Example
  35. ^^^^^^^^^^^^^^^^
  36. .. code:: python
  37. import os
  38. from magic_pdf.data.data_reader_writer import S3DataReader, S3DataWriter
  39. from magic_pdf.config.make_content_config import DropMode, MakeMode
  40. from magic_pdf.pipe.OCRPipe import OCRPipe
  41. bucket_name = "{Your S3 Bucket Name}" # replace with real bucket name
  42. ak = "{Your S3 access key}" # replace with real s3 access key
  43. sk = "{Your S3 secret key}" # replace with real s3 secret key
  44. endpoint_url = "{Your S3 endpoint_url}" # replace with real s3 endpoint_url
  45. reader = S3DataReader('unittest/tmp/', bucket_name, ak, sk, endpoint_url) # replace `unittest/tmp` with the real s3 prefix
  46. writer = S3DataWriter('unittest/tmp', bucket_name, ak, sk, endpoint_url)
  47. image_writer = S3DataWriter('unittest/tmp/images', bucket_name, ak, sk, endpoint_url)
  48. ## args
  49. model_list = []
  50. pdf_file_name = f"s3://{bucket_name}/{fake pdf path}" # replace with the real s3 path
  51. pdf_bytes = reader.read(pdf_file_name) # read the pdf content
  52. pipe = OCRPipe(pdf_bytes, model_list, image_writer)
  53. pipe.pipe_classify()
  54. pipe.pipe_analyze()
  55. pipe.pipe_parse()
  56. pdf_info = pipe.pdf_mid_data["pdf_info"]
  57. md_content = pipe.pipe_mk_markdown(
  58. "unittest/tmp/images", drop_mode=DropMode.NONE, md_make_mode=MakeMode.MM_MD
  59. )
  60. if isinstance(md_content, list):
  61. writer.write_string(f"{pdf_file_name}.md", "\n".join(md_content))
  62. else:
  63. writer.write_string(f"{pdf_file_name}.md", md_content)
  64. Check :doc:`../data/data_reader_writer` for more [reader | writer] examples