Skip to content

'w:ilvl' when parsing malformed docx numbering (suggested fix inside) #158

Closed

Description

@tosmart01

Description

When using python-mammoth (via markitdown), I encountered a KeyError: 'w:ilvl' while converting a .docx to Markdown.
The error is triggered when the input Word document contains malformed numbering without the w:ilvl attribute.

Traceback

markitdown data/demo.docx > document.md --keep-data-uris
Traceback (most recent call last):
File "/opt/conda/bin/markitdown", line 8, in
sys.exit(main())
File "/opt/conda/lib/python3.10/site-packages/markitdown/main.py", line 196, in main
result = markitdown.convert(
File "/opt/conda/lib/python3.10/site-packages/markitdown/_markitdown.py", line 274, in convert
return self.convert_local(source, stream_info=stream_info, **kwargs)
File "/opt/conda/lib/python3.10/site-packages/markitdown/_markitdown.py", line 328, in convert_local
return self._convert(file_stream=fh, stream_info_guesses=guesses, **kwargs)
File "/opt/conda/lib/python3.10/site-packages/markitdown/_markitdown.py", line 614, in _convert
raise FileConversionException(attempts=failed_attempts)
markitdown._exceptions.FileConversionException: File conversion failed after 1 attempts:

DocxConverter threw KeyError with message: 'w:ilvl'

Minimal Reproduction

WordFromLark (2).docx

from markitdown import MarkItDown
md = MarkItDown().convert("WordFromLark (2).docx").markdown
print(md)

Expected Behavior

Mammoth should gracefully handle malformed numbering without crashing, ideally defaulting to level 0.

Proposed Solution

I tested a patch that works reliably:

import warnings
from mammoth.docx.numbering_xml import _AbstractNumLevel

def _read_abstract_num_level(element):
    level_index = element.attributes.get("w:ilvl")
    if level_index is None:
        warnings.warn(
            "Missing 'w:ilvl' in abstract numbering level. Defaulting to level 0.",
            UserWarning
        )
        level_index = "0"

    num_fmt = element.find_child_or_null("w:numFmt").attributes.get("w:val")
    is_ordered = num_fmt != "bullet"
    paragraph_style_id = element.find_child_or_null("w:pStyle").attributes.get("w:val")

    return _AbstractNumLevel(
        level_index=level_index,
        is_ordered=is_ordered,
        paragraph_style_id=paragraph_style_id,
    )

This preserves existing functionality while:

Avoiding crashes on malformed input

Providing a clear UserWarning so developers know the .docx file is not fully valid

Environment

python-mammoth version: 1.9.0

Python version: 3.11

OS: macOS

Activity

  1. mwilliamson commented on Sep 11, 2025

    @mwilliamson
    Owner

    Thanks for the report. This should be fixed on master, and therefore the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions