Repository navigation
Conversation
MarkItDown's DocxConverter builds a table through mammoth HTML and then writes it with markdownify, which leaves a "|" inside a cell's text as it is. The reader takes that pipe for a column delimiter, so a cell holding "10 | USD" splits into two columns and every later cell shifts right. CSV (Tencent#5392), EPUB (Tencent#5523) and PPTX (Tencent#6019) already wrap their converter for this reason; DOCX still went through the stock converter. The fallback DocxParser escapes pipes itself, so only the default MarkItDown path was affected. DocxConverter hands the mammoth HTML to the _html_converter instance attribute, so replacing that attribute with one that escapes the cells first is enough: the conversion itself stays MarkItDown's, and a document without a table takes the untouched path.
This branch has not been deployed
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.
Description
A DOCX table cell that contains
|is read as two columns.Docx2Parserprefers MarkItDown, whoseDocxConverterbuilds the table through mammoth HTML and then writes it with markdownify; markdownify copies the character as it is, so the row gains a cell that the delimiter row does not have and every later cell shifts one column right.A Word table with the header
Plan,Price | Currency,Notescurrently comes out as:Expected:
The same problem was fixed for CSV (#5392), EPUB (#5523) and PPTX (#6019). Those formats register a converter that escapes the pipe before the table is written. DOCX still goes through the stock
DocxConverter, so it was left out. The fallbackDocxParserescapes pipes in_gfm_cell_textalready, which is why only the default MarkItDown path is affected — a document that fails over to the fallback renders the cell correctly today.Change
DocxConverter.converthands the mammoth HTML to the instance attribute_html_converter, soEscapedDocxConverterreplaces that attribute with anHtmlConverterthat escapes the cells first. The conversion logic itself is still MarkItDown's: nothing is copied from the library.The cell-level escaping is
MHTMLParser._escape_table_cell_pipes, which HTML, EPUB and MHTML documents already use, so DOCX now shares that implementation instead of adding a fourth copy.A document whose HTML has no
<tabletakes the untouched path, so there is no extra BeautifulSoup parse for the common case.Tests
docreader/tests/test_docx_table_cells.pymirrorstest_pptx_table_cells.py: a cell holdingPrice | Currencymust stay one column, and a plain table must render exactly as before.Without the converter registration the first test fails with the split row shown above and the second still passes, so the test pins the defect rather than the implementation.
Full suite on the locked environment (
uv sync --locked, Python 3.10.18, markitdown 0.1.3):The single error is
test_ssrf_proxy.test_webkit_redirects_and_subresources_use_proxy, which needs the Playwright WebKit build and fails the same way onmainwithout it.