How to work with merged cells in Excel with `openpyxl` in Python?

How to work with merged cells in Excel with openpyxl in Python

Import openpyxl module and read the workbook:

python 复制代码
import openpyxl

wb = openpyxl.load_workbook('attachments/device.xlsx')

Print merged cells:

python 复制代码
for ws, name in zip(wb.worksheets, wb.sheetnames):
    print("Sheet: ", name)
    print(ws.merged_cells)
复制代码
Sheet:  smoke transducer
A3:A8 A10:A13
Sheet:  temperature and humidity sensor
A4:A7 A9:A11
Sheet:  infrared sensor
A4:A7 A9:A12
Sheet:  Dc voltage transmitter
A4:A7 A9:A10
Sheet:  water depth sensor
A4:A5 A7:A11

Check the type of ws.merged_cells

python 复制代码
type(ws.merged_cells)
复制代码
openpyxl.worksheet.cell_range.MultiCellRange

Refer the below link for CellRange and MergedCellRange:

Check the type of ws.merged_cells.ranges, it is set.

python 复制代码
print(type(ws.merged_cells.ranges))
复制代码
<class 'set'>

Check type of ranges again:

python 复制代码
for cell_range in ws.merged_cells.ranges:
    print(type(cell_range))
    print(type(cell_range).__bases__)
    break
复制代码
<class 'openpyxl.worksheet.merge.MergedCellRange'>
(<class 'openpyxl.worksheet.cell_range.CellRange'>,)

Refer the link for cell method on sheet: https://openpyxl.readthedocs.io/en/stable/api/openpyxl.worksheet.worksheet.html#openpyxl.worksheet.worksheet.Worksheet.cell

We can use the cell method to get value:

python 复制代码
ws.cell(row=cell_range.min_row, column=cell_range.min_col).value
复制代码
technical experience

We can use unmerge_cells method to unmerged cells, see https://openpyxl.readthedocs.io/en/stable/api/openpyxl.worksheet.worksheet.html#openpyxl.worksheet.worksheet.Worksheet.unmerge_cells for more info.

Here is the full program

python 复制代码
for ws, name in zip(wb.worksheets, wb.sheetnames):
    merged_cells = list(ws.merged_cells.ranges)
    for cell_range in merged_cells:
        # Get the value of the merged cell
        cell_value = ws.cell(
            row=cell_range.min_row,
            column=cell_range.min_col,
        ).value
        
        # Unmerge cells
        ws.unmerge_cells(
            start_row=cell_range.min_row,
            start_column=cell_range.min_col,
            end_row=cell_range.max_row,
            end_column=cell_range.max_col,
        )
        
        # Fill value to the unmerged cells that is empty
        for row in range(cell_range.min_row, cell_range.max_row + 1):
            for col in range(cell_range.min_col, cell_range.max_col + 1):
                ws.cell(row=row, column=col).value = cell_value
相关推荐
TRACER~855 分钟前
项目实战:pyqt6实现拼豆图纸生成器
python·pyqt
锵锵锵锵~蒋7 分钟前
AI全托管处理EXCEL(并接入AI平台)
人工智能·excel·mcp·ai全托管·ai提效’
Flandern11118 分钟前
Go程序员学习AI大模型项目实战02:给 AI 装上“大脑”:从配置解包到流式生成的深度拆解
人工智能·后端·python·学习·golang
qq_2837200514 分钟前
Python 数据分析:Pandas+NumPy 超详细教程
python·数据分析·pandas
dragen_light16 分钟前
气象数据下载-Climate Data Store
python
许杰小刀19 分钟前
Python网络请求库,从 requests 到 httpx
开发语言·python·httpx
ZC跨境爬虫34 分钟前
Scrapy实战爬取5sing网站:Pipeline优化+全流程踩坑复盘,从报错到数据落地
前端·爬虫·python·scrapy
ZhuNian的学习乐园34 分钟前
LLM智能体调度:从ReAct到多智能体调度
人工智能·python·深度学习
喵叔哟37 分钟前
6.【.NET10 实战--孢子记账--产品智能化】--认证与安全包
python·安全·flask
小鱼~~44 分钟前
Jupyter Notebook 最常用快捷键
python·jupyter