How to detect if a cell is empty when reading Excel files using the xlrd library?
You could be explicit and check that sheet.cell_type(rowno, colno) in (xlrd.XL_CELL_EMPTY, xlrd.XL_CELL_BLANK)
but the docs state the value will be u''
where those are the case anyway.
Instead of using row_values
, you could also use row(n)
which returns a list of Cell
objects which have .value
and .cell_type
attributes.
sergzach
I am interested in Django, Javascript, sql and nosql databases, also programming bots, organizing human-machine processes. An example of my code: https://github.com/sergzach/readingproc. BTW: I am against the terrible war in Ukraine. I have never voted for Putin.
Updated on July 23, 2020Comments
-
sergzach almost 4 years
I handle Excel files using the functions row_values and col_values:
import xlrd workbook = xlrd.open_workbook( filename ) sheet_names = workbook.sheet_names() for sheet_name in sheet_names: sheet = workbook.sheet_by_name( sheet_name ) # ... row_values = sheet.row_values( rownum ) # ... col_values = sheet.col_values( colnum )
For example, I get col_values as list. What if I meet an empty cell in some column? For example a cell (1,1) is not empty, a cell (1,2) is empty and a cell (1,3) is not empty? How can I detect that the cell (1,2) is empty?
Is this true that I get a list with an empty string as a value of an empty cell (for most well-known programs which generate Excel files)?
-
Ali Tou over 3 yearsThis is almost 8 years later and I'm getting
'Cell' object has no attribute 'cell_type'
-
jack.py over 3 years@AliTou this may be a roundabout way, but I just convert the cell value to a string and compare it to an empty string (e.g
cell_value = str(cell.value); cell_empty = (cell_value == ""))