检查excel单元格是否为空(Python)

检查excel单元格是否为空(Python),python,excel,Python,Excel,我知道有类似的问题,但我不太明白他们的答案。感谢您的帮助 import xlrd path = ('E:\clean.xlsx') wb = xlrd.open_workbook(path) sheet = wb.sheet_by_index(0) is_empty = None if (sheet.cell(row=0, column=0).value) == None: print("empty") 如何检查excel文件中的特定单元格并检查其是否为空?谢谢。您

我知道有类似的问题,但我不太明白他们的答案。感谢您的帮助

import xlrd
path = ('E:\clean.xlsx')
wb = xlrd.open_workbook(path)
sheet = wb.sheet_by_index(0)
is_empty = None
if (sheet.cell(row=0, column=0).value) == None:
    print("empty")

如何检查excel文件中的特定单元格并检查其是否为空?谢谢。

您可以使用
sheet.cell\u值(rowx=row,colx=col)
检查值。但是,该表是一个数组。因此,如果值为null,代码将出错。因此,您需要使用
sheet.nrows
sheet.ncols

我相信有更好的方法来检查null或空。但是,以下操作应该有效:
if sheet.nrows>row和sheet.ncols>col和sheet.cell\u值(rowx=row,colx=col):

示例:

import xlrd
path = ('E:\clean.xlsx')

wb = xlrd.open_workbook(path)
sheet = wb.sheet_by_index(0)

def is_null_or_empty(sheet, row, col):
    return True if sheet.nrows > row and sheet.ncols > col and sheet.cell_value(rowx=row, colx=col) else False
  
print(f'has value for row {0} col {0}: {is_null_or_empty(sheet, 0, 0)}')
print(f'has value for row {0} col {1}: {is_null_or_empty(sheet, 0, 1)}')
has value for row 0 col 0: False
has value for row 0 col 1: True
输出:

import xlrd
path = ('E:\clean.xlsx')

wb = xlrd.open_workbook(path)
sheet = wb.sheet_by_index(0)

def is_null_or_empty(sheet, row, col):
    return True if sheet.nrows > row and sheet.ncols > col and sheet.cell_value(rowx=row, colx=col) else False
  
print(f'has value for row {0} col {0}: {is_null_or_empty(sheet, 0, 0)}')
print(f'has value for row {0} col {1}: {is_null_or_empty(sheet, 0, 1)}')
has value for row 0 col 0: False
has value for row 0 col 1: True
TypeError:cell()得到了一个意外的关键字参数“row”,我也得到了这个错误。