Openpyxl check for empty cell

前端 未结 4 1938
失恋的感觉
失恋的感觉 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:16

    To do something when cell is not empty add:

    if cell.value:
    

    which in python is the same as if cell value is not None (i.e.: if not cell.value == None:)

    Note to avoid checking empty cells you can use

    worksheet.get_highest_row()
    

    and

    worksheet.get_highest_column()
    

    Also I found it useful (although might not be a nice solution) if you want to use the contents of the cell as a string regardless of type you can use:

    unicode(cell.value)
    

提交回复
热议问题