Openpyxl check for empty cell

前端 未结 4 1933
失恋的感觉
失恋的感觉 2020-12-15 18:49

openpyxl seems to be a great method for using Python to read Excel files, but I\'ve run into a constant problem. I need to detect whether a ce

4条回答
  •  春和景丽
    2020-12-15 19:10

    The

    if cell.value is None:
    

    is the best option to use for this case.

    if you wish to use it in an iteration, you can use it like this:

    ws=wb.active
    names=ws['C']
    for x in names:
        if x.value is None:
            break
        print(x.value)
    

    The problem with ws.max_column and ws.max_row is that it will count blank columns as well, thus defeating the purpose.

提交回复
热议问题