Reputation: 119
I understand there are similar questions but I don't quite understand their answers. Any help is appreciated.
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")
How do I check for a specific cell in my excel file and check if it is empty? Thanks.
Upvotes: 1
Views: 509
Reputation: 4518
You can check the value using sheet.cell_value(rowx=row, colx=col)
. However the sheet is an array. As a result the code will error if the value is null. Therefore you'll need to check the indexes are inside the array using sheet.nrows
and sheet.ncols
I'm sure theres better way of checking null or empty. However the following should work: if sheet.nrows > row and sheet.ncols > col and sheet.cell_value(rowx=row, colx=col):
Example:
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)}')
Output:
has value for row 0 col 0: False
has value for row 0 col 1: True
Upvotes: 2