Skip to main content

Overview

The DocxConverter class converts Microsoft Word .docx files to Markdown, preserving style information (headings, bold, italic) and table structures. It extends HtmlConverter and uses mammoth to convert DOCX to HTML, then to Markdown.

Dependencies

Requires: mammoth

Accepted Formats

list
  • application/vnd.openxmlformats-officedocument.wordprocessingml.document
list
  • .docx

Class Definition

Constructor

Initializes the converter with an internal HtmlConverter instance.

Methods

accepts()

Returns True if the file has a .docx extension or the DOCX MIME type.

convert()

Converts a DOCX file to Markdown. Parameters:
BinaryIO
required
Binary stream of the DOCX file
StreamInfo
required
Metadata about the file (extension, MIME type, etc.)
str
Optional Mammoth style map to customize conversion. Allows mapping DOCX styles to specific HTML/Markdown elements.
Returns: DocumentConverterResult with converted Markdown Raises: MissingDependencyException if mammoth is not installed

Features

Preserved Elements

  • Headings - H1-H6 styles converted to Markdown headings
  • Text Formatting - Bold, italic, underline, strikethrough
  • Lists - Bulleted and numbered lists
  • Tables - Converted to Markdown tables with proper alignment
  • Links - Hyperlinks preserved
  • Images - Image references maintained (see options)

Pre-Processing

The converter applies pre-processing to the DOCX file before conversion (via pre_process_docx()) to normalize structure and improve conversion quality.

Example Usage

Basic Conversion

With Style Map

Output Example

Input DOCX with heading, paragraph, and table:

Conversion Pipeline

  1. Pre-process - Apply DOCX structure normalization
  2. Mammoth Conversion - Convert DOCX to HTML using Mammoth
  3. HTML to Markdown - Convert HTML to Markdown using HtmlConverter

Style Map Reference

Mammoth style maps allow fine-grained control over conversion:
See Mammoth documentation for complete style map syntax.

Implementation Details

Source Location

~/workspace/source/packages/markitdown/src/markitdown/converters/_docx_converter.py:31

Inheritance

  • Extends HtmlConverter for Markdown conversion
  • Leverages existing HTML-to-Markdown infrastructure

Limitations

  • Only supports .docx format (Office 2007+), not legacy .doc files
  • Complex formatting may not convert perfectly
  • Embedded objects (equations, SmartArt) have limited support
  • Comments and tracked changes are not preserved