Home Backend Development Python Tutorial Deep Dive into Microsoft MarkItDown

Deep Dive into Microsoft MarkItDown

Dec 26, 2024 pm 04:14 PM

What is MarkItDown?

MarkItDown is a Python package developed by Microsoft, designed to convert a variety of file formats into Markdown.

Since its debut, the library has skyrocketed in popularity, gaining over 25k GitHub stars within just two weeks! ?

Deep Dive into Microsoft MarkItDown

What Makes MarkItDown So Popular?

MarkItDown offers robust support for a wide array of file types, such as:

  • Office formats: Word, PowerPoint, Excel
  • Media files: Images (with EXIF data and descriptions), Audio (with transcription support)
  • Web and data formats: HTML, JSON, XML, CSV
  • Archives: ZIP files

Its ability to handle not just standard formats like Word but also multi-modal data makes it stand out. For example, it uses OCR and speech recognition to extract content from images and audio files.

The ability to convert anything into Markdown makes MarkItDown a powerful tool for LLM training. By processing domain-specific documents, it provides rich context for generating more accurate and relevant responses in LLM-powered applications.

Getting Started with MarkItDown

Using MarkItDown is incredibly straightforward - only 4 lines of code are needed:

from markitdown import MarkItDown

md = MarkItDown()
result = md.convert("test.xlsx")
print(result.text_content)
Copy after login
Copy after login

Here's some use cases of MarkItDown.

Converting a Word document generates clean and accurate Markdown:

Deep Dive into Microsoft MarkItDown

Even multi-tab Excel spreadsheets are handled with ease:

Deep Dive into Microsoft MarkItDown

ZIP archives? No problem! The library parses all files inside them recursively:

Deep Dive into Microsoft MarkItDown

Initially, image extraction might yield no results:

Deep Dive into Microsoft MarkItDown

This is because MarkItDown relies on an LLM to generate image descriptions. By integrating an LLM client, you can enable this feature:

from openai import OpenAI

client = OpenAI(api_key="i-am-not-an-api-key")

md = MarkItDown(llm_client=client, llm_model="gpt-4o")
Copy after login
Copy after login

With the configuration in place, image files can be successfully processed:

Deep Dive into Microsoft MarkItDown

Note: LLM won't deal with image-based PDFs. PDFs need OCR preprocessing to extract content.

Deep Dive into Microsoft MarkItDown

However, PDFs lose their formatting upon extraction, therefore headings and plain text are not distinguished:

Deep Dive into Microsoft MarkItDown

Limitations

MarkItDown isn’t without its limitations:

  • PDF files without OCR cannot be processed.
  • Formatting is not available when extracting from PDF files.

Nonetheless, as an open-source project, it’s highly customizable. Developers can easily extend its functionality due to its clean codebase.

How MarkItDown Works

MarkItDown’s architecture is straightforward and modular.

It has a DocumentConverter class, which defines a generic convert() method:

from markitdown import MarkItDown

md = MarkItDown()
result = md.convert("test.xlsx")
print(result.text_content)
Copy after login
Copy after login

Individual converters inherit from this base class and are registered dynamically:

from openai import OpenAI

client = OpenAI(api_key="i-am-not-an-api-key")

md = MarkItDown(llm_client=client, llm_model="gpt-4o")
Copy after login
Copy after login

This modular approach makes it easy to add support for new file types.

File Conversion Workflows

Office Documents

Office files are transformed into HTML using libraries like mammoth, pandas, or pptx, and then converted to Markdown with BeautifulSoup.

Deep Dive into Microsoft MarkItDown

Audio Files

Audio is transcribed with the speech_recognition library, which utilizes Google’s API.

(Microsoft, why not Azure here? ?)

Deep Dive into Microsoft MarkItDown

Images

Image processing involves generating a caption via an LLM prompt:
"Write a detailed description for this image."

Deep Dive into Microsoft MarkItDown

PDFs

PDFs are handled by the pdfminer library but lack built-in OCR. You must preprocess PDFs for text extraction.

Deep Dive into Microsoft MarkItDown

Deploying MarkItDown as an API

MarkItDown can run locally, but hosting it as an API unlocks additional flexibility, making it easy to integrate into workflows like Zapier and n8n.

Here’s a simple example of MarkItDown API using FastAPI:

class DocumentConverter:
    """Base class for all document converters."""

    def convert(
        self, local_path: str, **kwargs: Any
    ) -> Union[None, DocumentConverterResult]:
        raise NotImplementedError()
Copy after login

To call the API:

self.register_page_converter(PlainTextConverter())
self.register_page_converter(HtmlConverter())
self.register_page_converter(DocxConverter())
self.register_page_converter(XlsxConverter())
self.register_page_converter(Mp3Converter())
self.register_page_converter(ImageConverter())
# ...
Copy after login

Hosting the API at No Cost

Hosting Python APIs can be tricky. Traditional services like AWS EC2 or DigitalOcean require renting an entire server, which is always costly.

But now, you can use Leapcell.

It's a platform which can host Python codebase in the serverless way - it charges only per API call, with a generous free-tier usage.

Just connect your GitHub repository, define build and start commands, and you’re all set:

Deep Dive into Microsoft MarkItDown

Now you have a MarkItDown API that’s hosted in the cloud, ready for integration into your workflow, and most importantly, only charges when it's really called.


Start building your own MarkItDown API on Leapcell today! ?

Deep Dive into Microsoft MarkItDown

The above is the detailed content of Deep Dive into Microsoft MarkItDown. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

See all articles