Skip to content

fix(docx): preserve namespaces when repairing stylesheets - #2467

Merged
afourney merged 4 commits into
mainfrom
utf8_splitting
Sep 11, 2026
Merged

afourney merged 4 commits into
mainfrom
utf8_splitting

Conversation

@afourney

Copy link
Copy Markdown
Member

DOCX stylesheet preprocessing could make valid documents unconvertible. When  word/styles.xml  declares the WordprocessingML namespace both with a prefix and as the default namespace, BeautifulSoup serialization can strip namespace qualification from attributes such as  w:type . Mammoth then fails with  KeyError: 'w:type' , preventing extraction of the document’s contents.

Replace the stylesheet transformation with namespace-aware  lxml  processing:

• Match elements and attributes by namespace URI rather than literal prefix.
• Preserve existing repairs: default missing style types to  paragraph  and remove styles without IDs.
• Return the original stylesheet bytes when no repair is needed.
• Disable entity resolution and network access during parsing.

 lxml  is already a DOCX dependency and is imported locally, so this does not introduce a new dependency for other conversion formats.

Regression coverage includes real DOCX conversion through both regular and OCR paths, alternate namespace prefixes, unchanged valid stylesheets, and preservation of existing repair behavior. Four end-to-end cases fail before the fix with  KeyError: 'w:type'  and pass afterward; all 48 selected tests pass.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

BeautifulSoup can still corrupt stylesheet namespaces during the preceding double-strikethrough repair.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Updates DOCX stylesheet repair to preserve XML namespaces during conversion.

Changes:

  • Replaces BeautifulSoup stylesheet processing with namespace-aware lxml.
  • Preserves unchanged stylesheets and existing repairs.
  • Adds regular and OCR regression coverage.
File summaries
File Description
packages/markitdown/src/markitdown/converter_utils/docx/pre_process.py Implements namespace-aware style repair.
packages/markitdown/tests/test_docx_styles.py Adds stylesheet and end-to-end tests.
packages/markitdown-ocr/tests/test_docx_converter.py Tests OCR converter paths.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused implementation preserves existing behavior and is supported by comprehensive regression coverage.

Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@afourney
afourney merged commit 485a7b9 into main Sep 11, 2026
24 checks passed
@afourney
afourney deleted the utf8_splitting branch September 11, 2026 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants