convert_ms_office.rst 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. Convert Doc
  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. Command Line
  8. ^^^^^^^^^^^^^
  9. .. code:: python
  10. # replace with real ms-office file, we support MS-DOC, MS-DOCX, MS-PPT, MS-PPTX now
  11. magic-pdf -p a.doc -o output -m auto
  12. API
  13. ^^^^^^^^
  14. .. code:: python
  15. import os
  16. from magic_pdf.data.data_reader_writer import FileBasedDataWriter, FileBasedDataReader
  17. from magic_pdf.model.doc_analyze_by_custom_model import doc_analyze
  18. from magic_pdf.data.read_api import read_local_office
  19. from magic_pdf.config.enums import SupportedPdfParseMethod
  20. # prepare env
  21. local_image_dir, local_md_dir = "output/images", "output"
  22. image_dir = str(os.path.basename(local_image_dir))
  23. os.makedirs(local_image_dir, exist_ok=True)
  24. image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(
  25. local_md_dir
  26. )
  27. # proc
  28. ## Create Dataset Instance
  29. input_file = "some_doc.doc" # replace with real ms-office file, we support MS-DOC, MS-DOCX, MS-PPT, MS-PPTX now
  30. input_file_name = input_file.split(".")[0]
  31. ds = read_local_office(input_file)[0]
  32. ## inference
  33. if ds.classify() == SupportedPdfParseMethod.OCR:
  34. ds.apply(doc_analyze, ocr=True).pipe_ocr_mode(image_writer).dump_md(
  35. md_writer, f"{input_file_name}.md", image_dir)
  36. else:
  37. ds.apply(doc_analyze, ocr=False).pipe_txt_mode(image_writer).dump_md(
  38. md_writer, f"{input_file_name}.md", image_dir)