[Python] How to read merged cells in Excel with Python using XLRD library?

Discussão em 'Python' iniciado por Stack, Fevereiro 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to read merged cells of Excel with Python using xlrd.

    My Excel: (note that the first column is merged across the three rows)

    A B C
    +---+---+----+
    1 | 2 | 0 | 30 |
    + +---+----+
    2 | | 1 | 20 |
    + +---+----+
    3 | | 5 | 52 |
    +---+---+----+


    I would like to read the third line of the first column as equal to 2 in this example, but it returns ''. Do you have any idea how to get to the value of the merged cell?

    My code:

    all_data = [[]]
    excel = xlrd.open_workbook(excel_dir+ excel_file)
    sheet_0 = excel.sheet_by_index(0) # Open the first tab

    for row_index in range(sheet_0.nrows):
    row= ""
    for col_index in range(sheet_0.ncols):
    value = sheet_0.cell(rowx=row_index,colx=col_index).value
    row += "{0} ".format(value)
    split_row = row.split()
    all_data.append(split_row)


    What I get:

    '2', '0', '30'
    '1', '20'
    '5', '52'


    What I would like to get:

    '2', '0', '30'
    '2', '1', '20'
    '2', '5', '52'

    Continue reading...

Compartilhe esta Página