Excel Merged Cells Only Fill the Top-Left: How to Extract
When using openpyxl to iterate through Excel row data, developers often encounter a typical issue: in the Excel application, cells A2:A5 clearly display the sam
If you read Excel data with openpyxl, you may find that a merged range looks complete in Excel but returns mostly empty values in your code. This happens with department names, categories, and other labels that span several rows. Your code can then write incomplete records to a database or pass broken associations into later steps.
This guide explains why the values go missing and shows a safe way to fill them. It also covers the cases where automatic filling should stop and a person should check the file. You will get a decision table, a demonstration in memory, and a checklist you can reuse.
Note: The examples use synthetic data built in memory. Keep the original copies of important files, and check the results against the criteria in this guide before you rely on them.
How do you extract merged cell values in Excel? The short answer
Read the value from the top-left cell of each merged range, then copy it into every cell that the merge covers, and stop to check any range that breaks the rules in the table below.
| Your situation | What to do | Why |
|---|---|---|
| The top-left cell has a value and the other cells read as None | Fill that value across the whole merged region | openpyxl stores the value only in the top-left cell, so the rest need the value copied in |
| The top-left cell is empty or None | Log a warning and leave the cells as None | Filling would copy an empty value over possible business data, so a person should review it |
| The merge is larger than 10 rows or 5 columns | Pause automatic processing | The example thresholds mark an unusually large merge that needs a closer look |
| Two merged ranges overlap on the same row | Raise a structured conflict error and stop processing that row | Overlapping ranges make the correct value unclear |
| A row has a different number of fields from the others | Output the original row for comparison | This is treated as structural damage |
Why does a merged cell return None?
How openpyxl stores merged cells
When you read a merged range with openpyxl, the value is kept only in the top-left cell. The other cells in the range become MergedCell objects. These objects do not hold business data, and their value is None by default.
This is not file corruption. It comes from how openpyxl stores merged ranges, as described in the openpyxl.worksheet.merge module documentation.
What goes wrong without handling
In Excel, cells A2:A5 may all show the same department name. Your code, however, sees a value in A2 and None in A3 to A5. If you read the cells by coordinate without special handling, you lose that information.
The fix is to copy the top-left value into every sub-cell before you read the data. This restores the business meaning of the merged range.
A synthetic example: correct expansion and error handling
To make the problem concrete, build a worksheet in memory rather than loading an external file. This avoids dependence on real data.
The correct result
Suppose B2:B4 is merged with the value "R&D Department", and C2:C4 holds three employee names. The correct output is three complete records, each with the department set to "R&D Department".
If the code does not handle the merged range, the output looks like this:
[('研发部', '张三'), (None, '李四'), (None, '王五')]
Downstream steps such as database entry or translation then lose the link between each person and the department.
An empty top-left cell
Another common problem is a merged range whose top-left cell is empty. For example, D2:E3 may be merged with an empty string. Filling blindly would copy that empty value into all four cells and could hide the fact that data is missing.
A safer approach is to:
- Check whether the top-left value is None or blank.
- If it is, log a warning and skip the fill, so the original structure stays visible for manual review.
- If it is not, copy the value into every cell in the merged range.
For your own projects, prepare an .xlsx sample. Then replace the in-memory construction in the code below with load_workbook('your_file.xlsx'). Do not run the code directly on production files.
Memory-level Python implementation
The following example uses openpyxl to build a worksheet with merged cells in memory and shows the safe fill logic.
Note: Run this code with pinned dependency versions. If you process real files, change the loading method first.
# 依赖: openpyxl (请在自己的环境中锁定版本)
from openpyxl import Workbook
from openpyxl.cell.cell import MergedCell
def expand_merged_cells(ws):
"""将合并区域的值填充到所有子单元格,返回展开后的二维列表"""
# 先收集所有合并区域及其左上角值
merge_map = {}
for mcr in ws.merged_cells.ranges:
top_left_value = ws.cell(mcr.min_row, mcr.min_col).value
merge_map[(mcr.min_row, mcr.min_col, mcr.max_row, mcr.max_col)] = top_left_value
rows = []
for row in ws.iter_rows(min_row=1, max_row=ws.max_row, max_col=ws.max_column):
expanded_row = []
for cell in row:
val = cell.value
# 关键修正:MergedCell对象的value始终为None,必须通过范围判断继承值
if isinstance(cell, MergedCell):
for (r1, c1, r2, c2), merged_val in merge_map.items():
if r1 <= cell.row <= r2 and c1 <= cell.column <= c2:
val = merged_val
break
expanded_row.append(val)
rows.append(expanded_row)
return rows
# 合成示例:B2:B4 合并为“研发部”
wb = Workbook()
ws = wb.active
ws['A1'], ws['B1'], ws['C1'] = 'ID', 'Dept', 'Name'
ws['A2'], ws['B2'], ws['C2'] = 1, '研发部', '张三'
ws['A3'], ws['C3'] = 2, '李四'
ws['A4'], ws['C4'] = 3, '王五'
ws.merge_cells('B2:B4')
result = expand_merged_cells(ws)
assert result[1][1] == '研发部' # B2
assert result[2][1] == '研发部' # B3 应继承
assert result[3][1] == '研发部' # B4 应继承
assert result[1][2] == '张三'
Acceptance criteria and manual review triggers
Use the table below to decide what your code does with each merged range and when a person should check it.
| Observation of the phenomenon | processing decision | Whether manual review is required |
|---|---|---|
| has a non-empty value in the top-left corner of the merged region, and all subcells are None | automatically populates this value across the entire region | no |
| merged region’s top-left corner is an empty string or None | records a warning, retaining the original None status | is |
| merged region spans more than 10 rows or 5 columns | Marked as an exceptionally large merge, automatic processing is paused. | is |
| Multiple overlapping merged regions exist on the same line | throws a structured conflict error, terminating processing of this line | is |
| expands to a row where the number of fields differs from that of other rows | Treated as structural damage, the original line is output for comparison. | is |
Note: The thresholds above (10 rows and 5 columns) are custom parameters for this example. They are not industry standards. Set them according to your own tolerance for error. The code only checks logical correctness, so also test end-to-end performance against the layout complexity of your real documents.
Reference and next steps
Reference
Using this checklist for document translation
Before you translate a table, sort its contents into three groups: text, formulas, and business codes. After translation, check that the Chinese text is rendered correctly in the target language. Also check that formulas, identifiers, and citation links are unchanged.
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.
🎯 Which approach fits you?
If you read merged ranges and the top-left cell has a value
Copy that value into each cell of the range, then check the output against the expected row count.
If you process files with empty top-left cells
Log a warning and leave those cells as None, so a person can review them.
If you handle very large or overlapping merges
Pause automatic processing or raise a conflict error, then decide on the fix with the file owner.
❓ FAQ
1. Why are my merged cells None after I load the file with openpyxl?
openpyxl keeps the value only in the top-left cell of a merged range. The other cells become MergedCell objects whose value is None.
2. Can I fill every merged cell with the top-left value?
Yes, if the top-left cell has a value. If it is empty or None, log a warning and skip the fill so the missing data stays visible.
3. Should I run this code on my production files?
Test it on a copy of a real file first, and keep the original.
4. Are the 10-row and 5-column limits standard?
No. They are example parameters. Set your own limits based on how much error your project can tolerate.
📝 Summary
Merged cells in openpyxl keep their value only in the top-left cell, so you need to copy that value across the range before reading the data. Stop and review any range that is empty, too large, overlapping, or structurally inconsistent. Test the approach on real samples first.
Try it now: upload a document and get it translated with the layout intact


