8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Openpyxl 結合セルのValue取得

Posted at

#ググっても中々出てこないもしくはopenpyxlのバージョン違いでそのまま使えないから、書いておく

openpyxlのSheet型とCell型を引数として渡せば、そのCellは結合セルの場合は結合セルの先頭のセルのValueを返す、結合セルではなかったらそのままCellのValueを返す。

openpyxl version 2.6

getMergedCellValue.py
def getMergedCellValue(sheet, cell):
    cell_index = cell.coordinate
    for range_ in sheet.merged_cells.ranges:
        merged_cells = list(openpyxl.utils.rows_from_range(str(range_)))
        for row in merged_cells:
            if cell_index in row:
                return sheet[merged_cells[0][0]].value
    return sheet[cell_index].value
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?