markitdown icon indicating copy to clipboard operation
markitdown copied to clipboard

Enhance _markitdown.py to support embedding images in markdown

Open MauroDruwel opened this issue 1 year ago • 4 comments

This update enhances the DocxConverter class by adding functionality to extract and embed images when converting DOCX files to Markdown. The changes include:

  • convert_image method: Handles image extraction, sanitizes the filename, and saves the image as a PNG in the specified output directory.
  • convert method: Integrates the convert_image method with the Mammoth library's HTML conversion process, ensuring images are extracted and included in the final output.

MauroDruwel avatar Dec 23 '24 15:12 MauroDruwel

@microsoft-github-policy-service agree

MauroDruwel avatar Dec 23 '24 15:12 MauroDruwel

Looks promising. @MauroDruwel Can you please add some test cases?

Also, sanitizing filenames is a task that will come up a lot (and my already be implemented). I'm going to ask around for advice on how to handle this broadly and robustly (without regular expressions). Filenames may also have other restrictions (e.g., length etc.) on some OSs.

afourney avatar Jan 03 '25 21:01 afourney

Hi @afourney,

I've added the following improvements:

  1. Test cases: I've included some test cases, which you can view in the result here.
  2. Filename formatting: I've replaced spaces with underscores in filenames to ensure compatibility.
  3. Length limit checker: A checker has been added to enforce a length limit for filenames.
  4. File existence check: Now the script checks if the file already exists and appends a counter to avoid overwriting.
  5. Alt text formatting: I've fixed an issue where newlines in the image alt_text were causing images not to show by removing them.

Let me know if you need any further adjustments!

MauroDruwel avatar Jan 04 '25 12:01 MauroDruwel

Hi @afourney, is there anything left that I need to do?

MauroDruwel avatar Jan 18 '25 21:01 MauroDruwel