使用 Python 结束 Excel 中的合并单元格



我正在使用xlrd包来解析Excel电子表格。我想获取合并单元格的结束索引。

  A   B   C
  +---+---+----+
1 | 2 | 2 | 2  |
  +   +---+----+
2 |   | 7 | 8  |
  +   +---+----+
3 |   | 0 | 3  |
  +   +---+----+
4 |   | 4 | 20 |
  +---+---+----+
5 |   | 2 | 0  |
  +---+---+----+

给定行索引和列索引,我想知道合并单元格的结束索引(如果合并)

在此示例中为 (行,列)=(0,0) ;结束 = 3

您可以使用

工作表对象merged_cells属性:https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html?p=4966#sheet.Sheet.merged_cells-attribute

它返回已合并的单元格的地址范围列表。

如果只想获取垂直合并单元格的结束索引:

def is_merged(row, column):
    for cell_range in sheet.merged_cells:
        row_low, row_high, column_low, column_high = cell_range
        if row in xrange(row_low, row_high) and column in xrange(column_low, column_high):
            return (True, row_high-1)
    return False

最新更新