Skip to main content

Overview

The HtmlConverter class converts HTML and XHTML documents to Markdown. It uses BeautifulSoup for parsing and a custom Markdownify implementation for conversion. This converter is also used as a base class for other converters (DOCX, PPTX, EPUB) that produce HTML as an intermediate format.

Dependencies

Included in base install: beautifulsoup4, markdownify

Accepted Formats

list
  • text/html
  • application/xhtml*
list
  • .html
  • .htm

Class Definition

Methods

accepts()

Returns True for HTML/XHTML files based on extension or MIME type.

convert()

Converts HTML from a file stream to Markdown. Parameters:
BinaryIO
required
Binary stream of the HTML file
StreamInfo
required
Metadata about the file (must include charset if not UTF-8)
Returns: DocumentConverterResult with Markdown and extracted title

convert_string()

Convenience method to convert HTML string directly to Markdown. Parameters:
str
required
HTML content as string
str
Optional URL for the HTML content (used in StreamInfo)
Returns: DocumentConverterResult with Markdown

Features

HTML Cleaning

Before conversion, the HTML is cleaned:
  1. Script Removal - All <script> tags removed
  2. Style Removal - All <style> tags removed
  3. Body Extraction - Only <body> content processed (if present)

Converted Elements

  • Headings - <h1>-<h6># headings
  • Paragraphs - <p> → Paragraphs with blank lines
  • Lists - <ul>, <ol> → Markdown lists
  • Links - <a>[text](url)
  • Images - <img>![alt](src)
  • Tables - <table> → Markdown tables
  • Code - <code>, <pre> → Inline/block code
  • Emphasis - <em>, <strong>*italic*, **bold**
  • Blockquotes - <blockquote>> quotes
  • Horizontal Rules - <hr>---

Title Extraction

Extracted from <title> tag and returned in DocumentConverterResult.title.

Character Encoding

Respects stream_info.charset, defaults to UTF-8:

Example Usage

From File

From String

Output:

With Tables

Output:

Usage as Base Class

Many converters extend HtmlConverter to leverage HTML-to-Markdown conversion:
Used by:
  • DocxConverter - Word documents via mammoth
  • PptxConverter - Tables from PowerPoint
  • XlsxConverter / XlsConverter - Excel tables via pandas HTML
  • EpubConverter - EPUB content files

Implementation Details

Source Location

~/workspace/source/packages/markitdown/src/markitdown/converters/_html_converter.py:20

Conversion Pipeline

  1. Parse HTML
  2. Clean Content
  3. Extract Body
  4. Extract Title

Custom Markdownify

Uses _CustomMarkdownify class (from _markdownify.py) which extends the markdownify library with custom conversion rules for better Markdown output quality.

Advanced Options

The converter accepts options passed to _CustomMarkdownify:

Use Cases

Web Scraping

Documentation Conversion

Email Conversion

Limitations

  • Complex CSS layouts not preserved
  • JavaScript-rendered content not processed
  • Forms and interactive elements lost
  • Nested tables may have formatting issues
  • Some HTML5 semantic elements treated as divs
  • Embedded media (video, audio) becomes links only