convert_doc.rst 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. Convert Word
  2. =============
  3. .. admonition:: Warning
  4. :class: tip
  5. When processing MS-Office files, we first use third-party software to convert the MS-Office files to PDF.
  6. For certain MS-Office files, the quality of the converted PDF files may not be very high, which can affect the quality of the final output.
  7. .. code:: python
  8. import os
  9. from magic_pdf.data.data_reader_writer import FileBasedDataWriter, FileBasedDataReader
  10. from magic_pdf.model.doc_analyze_by_custom_model import doc_analyze
  11. from magic_pdf.data.read_api import read_local_office
  12. # prepare env
  13. local_image_dir, local_md_dir = "output/images", "output"
  14. image_dir = str(os.path.basename(local_image_dir))
  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. # proc
  20. ## Create Dataset Instance
  21. input_file = "some_doc.doc" # replace with real ms-office file
  22. input_file_name = input_file.split(".")[0]
  23. ds = read_local_office(input_file)[0]
  24. ds.apply(doc_analyze, ocr=True).pipe_ocr_mode(image_writer).dump_md(
  25. md_writer, f"{input_file_name}.md", image_dir
  26. )