DEV Community

Lank_M
Lank_M

Posted on

Four DOCX converters agreed on screen and disagreed in the markup

On one self-made DOCX, four converters put the same merged-cell table on screen in the same shape, and the HTML underneath did not match. Two of them wrote 2, 5, 3 and 3 cells for the four rows of that table; the other two wrote 2, 5, 4 and 5. Both look right in a browser, so an acceptance gate made of screenshots waves both through, and whatever reads the table next gets two different documents.

The scope is narrow. One sample document, 43,790 bytes, built on purpose: headings, lists, two generated PNGs, header, footer, a footnote, and three tables, horizontal merges, vertical merges, and a mixed one at 5 columns by 4 rows. One Apple-silicon Mac, a desktop Chromium, one run per tool. The four were Mammoth 1.13.0, pandoc 3.13, docx-preview 0.4.1, and the browser-side DOCX to HTML on ImgIng https://imging.ai/ which ran without signing in. A forty-line reconstruction script went in as a control. Office formats are not the part of that product I work on, so everything below is read off the outputs.

The column count lives in tblGrid and the rowspan does not exist

Two facts about the format decide the whole comparison. A table's column count comes from the w:gridCol children of w:tblGrid, not from how many w:tc a row happens to have, and a horizontal merge is a plain number that copies straight into colspan. A vertical merge has no equivalent, because OOXML carries no rowspan field at all.

<w:gridCol w:w="1728"/> ... <!-- five of these inside w:tblGrid = five grid columns -->
<w:tc><w:tcPr><w:gridSpan w:val="2"/></w:tcPr> ...   <!-- becomes colspan="2" -->
<w:tc><w:tcPr><w:vMerge w:val="restart"/></w:tcPr> ... <!-- the merge starts here -->
<w:tc><w:tcPr><w:vMerge/></w:tcPr><w:p/></w:tc>        <!-- continuation, no w:val -->
Enter fullscreen mode Exit fullscreen mode

So a row holding 2 cells is correct against 5 grid columns, and the spans a converter emits are a reconstruction, not a copy. That is where the two groups part ways.

The extra cells are hidden, not wrong

In the mixed table, Mammoth, pandoc and the control script emitted exactly the expected 2, 5, 3, 3. docx-preview and the browser-side converter emitted 2, 5, 4, 5, and the three extra cells are empty <td> elements carrying display:none. The purely vertical table behaved the same way: 3, 3, 2, 2 from the first group, 3, 3, 3, 3 from the second, the two extras hidden again. Walk any of these outputs with a running account of rowspan carry-over and every row comes out at exactly five: on a well-formed file all of them got the merge values right. The familiar claim that open-source libraries cannot handle merged cells did not reproduce here, on this sample and these versions.

One detail from the same run: the JavaScript and the Python builds of Mammoth produced byte-identical files, both 10,167 bytes.

One missing restart split the failure two ways

The only real breakage this round came from the document, not the tools. In a variant of the sample, the first cell of the vertically merged group carries a bare <w:vMerge/> instead of <w:vMerge w:val="restart"/>, every other byte left alone. Mammoth 1.13.0 and pandoc 3.13 both treated the header cell one row above as the start of the merge, gave it rowspan="4", and dropped the text of the cell that should have started the merge: that string is nowhere in the output. docx-preview 0.4.1 and the browser-side converter kept the text and dropped the merge, leaving four plain rows. Neither group raised an error. One side loses characters, the other loses structure, and a single non-standard attribute is enough. Why the split runs in that direction I can't tell from the outputs alone, and I didn't go reading either implementation to find out.

Assert on the grid and on the text

That leaves two checks worth putting in CI for this step, both cheap. Rebuild the grid: take the column count from w:tblGrid, add up colspan per row, carry each rowspan=n down into the next n-1 rows, and require every row to come out exactly equal to the column count. Drop cells styled display:none before counting, otherwise the second group reports a row occupying six slots out of five and you spend an afternoon on a non-problem. Then diff the content: pull every w:t string out of document.xml and look for each one in the converted HTML. In this round the text check was the only thing that caught the missing string. The script that produced the expected grid is forty-odd lines and deliberately minimal: one anchor per column slot pointing at the cell still merging downward, no support for w:tblHeader, nested tables, w:tcBorders or diagonal headers.

All of this is one document, one machine, one run each, and the sample's footnote and header were written into the package by script rather than saved out of a real Word, so the file's own standing is partly inferred. A real business document with floating shapes, equations and section breaks could reorder the result. What I would keep anyway: the gate on a table conversion is the reconstructed grid plus the text inventory, not how the page looks.

Top comments (0)