openpyxl检查空单元格 [英] openpyxl check for empty cell

查看:4957
本文介绍了openpyxl检查空单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

openpyxl似乎是使用Python读取Excel文件的一个很好的方法,但我遇到了一个常见的问题。我需要检测一个单元格是否为空,但似乎无法比较任何单元格的属性。我试着作为一个字符串并使用但是没有工作。单元格的类型为空时为None,或NoneType,但我不知道如何比较一个对象。

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 cell is empty or not, but can't seem to compare any of the cell properties. I tried casting as a string and using "" but that didn't work. The type of cell when it is empty is None, or NoneType but I can't figure out how to compare an object to that.

建议?我知道openpyxl正在开发中,但也许这是一个普通的Python问题。

Suggestions? I understand openpyxl is under development, but maybe this is more a general Python problem.

推荐答案

添加:

if cell.value:

这在python中是相同的,如果单元格的值不是无(即:如果不是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()

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)

这篇关于openpyxl检查空单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆