Skip to content
MyDSHMy DSH Field Notes
中文
Independently maintained; not affiliated with DeepSeek. No association with mydsh.dev or other MyDSH sites.

Home/Workspace & files/Reading files & formats

Merged cells: xlsx shows the merge, csv just gives you blanks

The same table as csv and as xlsx produces a different structure in the model's head, especially monthly reports with merged headers.

VerifiedVersion and environmentChecked 2026-11-14
Tool
openpyxl with Python 3.13
Sample
a workbook with two merged ranges (A1:D1, B5:C5)
Checked
2026-11-14

Copyable command

python -c "import openpyxl,sys;w=openpyxl.load_workbook(sys.argv[1]);print([str(r) for r in w.active.merged_cells.ranges])" book.xlsx

A minimal example makes it obvious: merge the header A1:D1 and add another merge at B5:C5.

Reading the xlsx back:

('2026-11 revenue', None, None, None)
('Date', 'Item', 'Amount', 'Note')
('2026-11-01', 'Cake', 1280, None)
(None, None, None, 'subtotal above')
(None, 'merged note', None, None)
('2026-11-02', 'Loose tea', 960, None)

The same content as csv, read back:

row0: '2026-11 revenue,,,'
row1: 'Date,Item,Amount,Note'
row2: '2026-11-01,Cake,1280,'
row3: ',,,subtotal above'
row4: ',merged note,,'
row5: '2026-11-02,Loose tea,960,'

Where they differ

xlsxcsv
Merge informationmerged_cells.ranges tells you which cells are one cellLost entirely, becoming empty fields
Non-top-left cellReads as None (the value lives only top-left)Empty string, indistinguishable from genuinely unfilled
Empty string versus empty cellBoth read as NoneBoth are adjacent commas
Recovering header hierarchyYes, inferred from merged rangesNo; you see one line of text and three blanks

The second row is the killer: in csv, 'merged note' looks like it sits in column B with A and C empty. You cannot tell whether it was a merged centred caption or a real B value with neighbours blank. In xlsx you just look at the merged ranges.

Practical trade-off

  • Structure matters (header tiers, group subtotals): send xlsx and require it to report merged ranges first.
  • Only data rows (a clean two-dimensional table): csv is cheaper in tokens and carries no merge ambiguity.
  • Unsure: send xlsx for a structure description, then convert to csv for the data work. Two steps beats one.
One counter-intuitive detail: an empty string written into a cell reads back as None, not as an empty string. So 'this cell is blank' and 'this cell holds an empty value' are indistinguishable at read time — add an explicit marker column if you need to tell them apart.

What counts as working

Check every line. All of them pass or the job is not done.

  • You can state how many merged ranges the workbook has
  • You know non-top-left cells read as None
  • With csv you did not treat a merged caption as a column value

Where it fails

Symptom, cause, what to do — written in the order it actually happens.

SymptomCauseWhat to do
Everything reads as NoneReading the wrong sheet, or data lives elsewherePrint the sheet names first, then target the right one
A merged range shows one value onlyThat is what merging means: the value lives top-leftUnmerge and fill down first if you need every cell populated

Sources

Verified means measured here. Compiled means every line carries a source.