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
相关推荐
编程指南针2 小时前
2026新选题-基于Python的老年病医疗数据分析系统的设计与实现(数据采集+可视化分析)
开发语言·python·病历分析·医疗病历分析
reasonsummer3 小时前
【办公类-116-01】20250929家长会PPT(Python快速批量制作16:9PPT相册,带文件名,照片横版和竖版)
java·数据库·python·powerpoint
拉姆哥的小屋3 小时前
基于提示学习的多模态情感分析系统:从MULT到PromptModel的华丽升级
python·深度学习·学习
蒋星熠3 小时前
爬虫与自动化技术深度解析:从数据采集到智能运维的完整实战指南
运维·人工智能·爬虫·python·深度学习·机器学习·自动化
大翻哥哥6 小时前
Python 2025:异步革命与AI驱动下的开发新范式
开发语言·人工智能·python
hhzz6 小时前
Pythoner 的Flask项目实践-在web页面实现矢量数据转换工具集功能(附源码)
前端·python·flask
学习的学习者6 小时前
CS课程项目设计19:基于DeepFace人脸识别库的课堂签到系统
人工智能·python·深度学习·人脸识别算法
悠哉悠哉愿意7 小时前
【数据结构与算法学习笔记】双指针
数据结构·笔记·python·学习·算法
MoRanzhi12037 小时前
5. Pandas 缺失值与异常值处理
数据结构·python·数据挖掘·数据分析·pandas·缺失值处理·异常值处理
程序员的奶茶馆8 小时前
Python 字典速查:键值对操作与高频函数
python·面试