Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if a cell is empty in openpyxl python

I'm making a conditional statement in openpyxl Python to check if a cell is empty. Here's my code:

newlist = []
looprow = 1
print ("Highest col",readex.get_highest_column())
getnewhighcolumn = readex.get_highest_column()        
for i in range(0, lengthofdict):
    prevsymbol = readex.cell(row = looprow,column=getnewhighcolumn).value
    if prevsymbol == "None":
        pass
    else:
        newstocks.append(prevsymbol)
        looprow += 1
    #print (prevsymbol)
print(newlist)

I tried if prevsymbol == "": and if prevsymbol == null: to no avail.

like image 420
Newboy11 Avatar asked Oct 19 '25 05:10

Newboy11


1 Answers

You compare prevsymbol with str "None", not None object. Try

if prevsymbol == None:

Also here

prevsymbol = readex.cell(row = looprow,column=getnewhighcolumn).value

you use looprow as row index. And you increment looprow only if cell.value is not empty. Here

newstocks.append(prevsymbol)

you use newstocks instead of newlist. Try this code

newlist = []
print ("Highest col",readex.get_highest_column())
getnewhighcolumn = readex.get_highest_column()        
for i in range(0, lengthofdict):
    prevsymbol = readex.cell(row = i+1,column=getnewhighcolumn).value
    if prevsymbol is not None:
        newlist.append(prevsymbol)
print(newlist)
like image 116
kvorobiev Avatar answered Oct 21 '25 18:10

kvorobiev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!