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
相关推荐
缺点内向4 小时前
如何在C#中添加Excel文档属性?
开发语言·数据库·c#·.net·excel
虹科网络安全4 小时前
艾体宝干货 | Redis Python 开发系列#6 缓存、分布式锁与队列架构
redis·python·缓存
猎人everest4 小时前
Django Rest Framework (DRF) 核心知识体系梳理与深度讲解
后端·python·django
卿雪4 小时前
缓存异常:缓存击穿、缓存穿透、缓存雪崩 及其解决方案
java·数据库·redis·python·mysql·缓存·golang
背心2块钱包邮4 小时前
第3节——differentiation rules(求导法则)
人工智能·python·matplotlib·scipy
white-persist4 小时前
【攻防世界】reverse | answer_to_everything 详细题解 WP
c语言·开发语言·汇编·python·算法·网络安全·everything
shouchaobao4 小时前
仓库房进销存Excel模板合集:商品采购+出入库+库存统计一站式管理,适配仓库管理员/中小企业/个体商户
excel
Ttang235 小时前
【AI篇3】在Java项目中调用大模型API
java·人工智能·microsoft·ai·api
高频交易dragon5 小时前
python缠论形态分析过程
开发语言·网络·python
小二·5 小时前
Windows 11 官方系统安装与重装完整教程(2025年最新版)
windows·microsoft