Merged Word Cells Read as Duplicates? Here's Why
merged cells appear repeatedly in layout grid access and cannot be deduplicated by text. This synthetic example constructs only horizontal merges; omitting grid
Merged cells in Word tables often make a single value look like several. If you parse tables with python-docx and get repeated text, this guide explains why it happens and how to read the table by its layout grid instead of its visible shape. It is written for developers and document operations staff who clean or extract data from Word tables.
You will learn how Word stores merged cells, what goes wrong when you read row.cells directly, and how to map each cell to its grid position. You will also see where automated parsing should stop and a person should check the result.
The examples use synthetic samples. Keep your original copies, and verify output against the criteria in this guide before you process important documents.
Why does a merged cell show up twice? The short answer
Word stores each table row on a layout grid, so python-docx returns one entry for each grid column a merged cell covers. Map cells by grid position, and deduplicate by cell object rather than by text.
| Your situation | What to do | Why |
|---|---|---|
| A header is merged across two columns and appears twice | Map each cell to its grid position | row.cells returns the grid view, so one merged cell appears once per grid column it covers |
| You need to remove duplicates | Deduplicate after parsing, using cell object identity | Two separate, unmerged cells can have identical text, so text-based deduplication loses data |
| A row skips grid columns | Account for grid_cols_before and grid_cols_after | Without this, values shift left and the column indices of later rows are wrong |
| The table came from a PDF or pasted HTML | Flag it for manual review | Its XML may lack key attributes such as grid_cols_before |
| The table is nested inside another cell | Apply the grid logic to each inner table separately | The outer row and column counts do not apply to the inner tables |
Understanding the problem
Which problem comes first?
Merged cells appear repeatedly when you access the layout grid, and you cannot deduplicate them by text. This example builds only horizontal merges. Omitting grid columns requires a genuine, valid OOXML sample. You cannot simulate it by adding attributes to rows.
Why does the parsed table contain extra data?
When you iterate through a Word table that has merged cells, the most common problem is not missing content. It is reading too much. For example, a header row that visually spans two columns, such as "Project Name", is returned twice in the loop, once for each grid position it covers. If you store that text directly, downstream cleaning steps may treat one value as two separate records.
Why it happens
This is not a bug in the library. It reflects the difference between Word's underlying table model and what you see on the page.
- A Word table is an invisible layout grid, and every cell must align to it.
- A merge creates a visual object that spans several grid coordinates. In the XML, each of those coordinates must still be occupied to keep the table's structure intact.
row.cellstherefore returns the grid view, not the visual view.- To handle tables correctly, you must first accept this behaviour. The program reads grid indices, not rendered tiles.
What goes wrong with direct reading
If you use [c.text for c in row.cells] without handling grid_cols_before and grid_cols_after, the first row becomes ["标题A", "标题B"]. The column indices of every later row then shift left by one. No error is raised, but the meaning of the whole table is corrupted.
Nested tables need extra care. This example handles only the top-level grid. For recursive parsing, apply the same grid-alignment logic to each cell separately. Do not assume that the outer row and column counts apply to the inner tables.
Python parsing based on the layout grid
The core idea is that the duplication is correct. In the example, the merged value appears twice because it really occupies both positions (1,0) and (1,1) in the layout grid.
The example builds its inputs in memory. Dependencies are listed in the import section.
from docx import Document
from io import BytesIO
doc = Document()
table = doc.add_table(rows=2, cols=3)
table.cell(0, 0).merge(table.cell(0, 1)).text = "Group A"
table.cell(0, 2).text = "Total"
for cell, text in zip(table.rows[1].cells, ["same", "same", "20"]):
cell.text = text
out = BytesIO()
doc.save(out)
out.seek(0)
restored = Document(out).tables[0]
rows = [[cell.text for cell in row.cells] for row in restored.rows]
assert rows[0] == ["Group A", "Group A", "Total"]
assert rows[1] == ["same", "same", "20"]
assert len(rows[1]) == 3
assert restored.rows[0].cells[0]._tc is restored.rows[0].cells[1]._tc
assert restored.rows[1].cells[0]._tc is not restored.rows[1].cells[1]._tc
Deduplicate safely
Critical warning: If your business logic requires deduplication, do it after parsing. Base the judgement on the cell objects themselves, for example with id(cell). Never deduplicate on text alone. Two separate, unmerged cells can have identical text, and text-based deduplication would delete real data.
Acceptance criteria and manual review boundary
The code can resolve grid alignment, but it cannot decide whether a table is semantically complete. Note that "the code demonstration passed" only means the grid indices line up. It does not mean the extracted content is ready to use.
Decide how to handle each exception
| Observation | What to do | What not to infer |
|---|---|---|
| The same cell is repeated | Treat it as a grid mapping | Do not treat it as a duplicate translation of a merged entity |
| Two text segments are identical | Keep two identity slots | Do not deduplicate by text |
| A grid column is omitted | Provide a valid OOXML sample | Do not forge row attributes to test it |
Where automated parsing does not apply
- This method applies only to Word's native table model.
- Tables converted from PDFs, or created by pasting HTML, may have incomplete XML. Key attributes such as
grid_cols_beforemay be missing. - In those cases, the automated results cannot be trusted. Flag them for manual verification.
- If a table uses complex text wrapping or absolute positioning, the layout grid itself is no longer valid. The matrix the code produces will deviate significantly from the visual layout.
📊 At a glance
| Table source | Automated grid parsing | Manual review |
|---|---|---|
| Native Word table | Applies. Code can align grid indices | Still needed to confirm semantic completeness |
| PDF-converted or pasted HTML table | Results cannot be trusted if key attributes are missing | Required. Flag and verify |
| Table with complex wrapping or absolute positioning | The layout grid is invalid and the matrix will differ from the visual layout | Required |
| Nested table | Apply grid logic to each inner table separately | Check each level |
🎯 Which approach fits you?
If you parse native Word tables with merged headers
Map each cell by its grid position with grid_cols_before and grid_cols_after, and check the output against your original file.
If you need to remove duplicate values
Deduplicate after parsing, using cell object identity. Do not use text as the key.
If your tables came from PDFs or HTML paste
Expect incomplete XML. Route these tables to manual verification before you trust the output.
If your tables contain nested tables
Process each inner table with its own grid logic. Do not reuse the outer row and column counts.
Use this checklist for document translation
Translation work often mixes content problems with formatting problems. Keep checks for styles, headers and tables in a separate delivery checklist. Reviewers will then know exactly where to look.
OmniDoc translates PDF, Word, PowerPoint and Excel files: scanned pages are recognized first, table cells are translated one by one so tables keep their rows and columns, and a PDF comes back as both a bilingual and a translation-only file. Paid translation adds a glossary and context translation. For the table data alone, OmniDoc's free extraction finds each table's header and cells in PDFs and scans and writes them out as Markdown tables and JSON; common merged headers are recognized. If your team wants to start from real files, it is worth evaluating with your own samples.
References and next steps
- python-docx official documentation: Working with Tables
- Open XML SDK Documentation: Table Grid Specification (Microsoft Official Technical Document)
❓ FAQ
1. Why does row.cells return a merged cell more than once?
It returns the layout grid view, which has one entry for each grid column a cell covers. A header merged across two columns therefore appears twice.
2. Can I remove the duplicates by comparing text?
No. Two separate, unmerged cells can contain the same text, so you would delete real data. Deduplicate by cell object identity after parsing.
3. What should I do with a table converted from a PDF?
Flag it for manual verification. Its XML may lack key attributes such as grid_cols_before, so automated results cannot be trusted.
4. Does this method handle nested tables?
Not on its own. The example handles only the top-level grid. Apply the same grid logic to each inner table separately.
📝 Summary
Merged cells are repeated because Word's layout grid keeps every grid position occupied. Read tables by grid position, deduplicate by cell object rather than text, and send PDF-converted, HTML-pasted or complex tables to manual review. Verify the results on copies of your own files before you rely on them.
Try it now: upload a document and get it translated with the layout intact


