convert_docx.rst 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. Convert DocX
  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. # make sure the file have correct suffix
  11. magic-pdf -p a.docx -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. # prepare env
  20. local_image_dir, local_md_dir = "output/images", "output"
  21. image_dir = str(os.path.basename(local_image_dir))
  22. os.makedirs(local_image_dir, exist_ok=True)
  23. image_writer, md_writer = FileBasedDataWriter(local_image_dir), FileBasedDataWriter(
  24. local_md_dir
  25. )
  26. # proc
  27. ## Create Dataset Instance
  28. input_file = "some_docx.docx" # replace with real ms-office file
  29. input_file_name = input_file.split(".")[0]
  30. ds = read_local_office(input_file)[0]
  31. ds.apply(doc_analyze, ocr=True).pipe_ocr_mode(image_writer).dump_md(
  32. md_writer, f"{input_file_name}.md", image_dir
  33. )