fix(docx): preserve namespaces when repairing stylesheets - #2467
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
🟡 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.