Reputation: 13
I'm using python openpyxl to extract specific data from an xlsx file to another xlsx. I defined a function which extracts the data I need and then I ran it using a while loop and told it to stop when it finds an empty cell.
But for some reason it gives me this error: Row numbers must be between 1 and 1048576
Here is my code:
x=3; y=2; z=4; i=5
def line():
c1 = ws1.cell(row = z, column = 1)
ws2.cell(row = y, column = 1).value = c1.value
c2 = ws1.cell(row = i, column = 2)
ws2.cell(row = y, column = 2).value = c2.value
c3 = ws1.cell(row = i, column = x)
ws2.cell(row = y, column = 3).value = c3.value
while ws1.cell(row=i, column=x+2).value != "":
line()
y+=1
x+=2
i+=1
else:
sys.exit()
What am I doing wrong?
Upvotes: 0
Views: 4065
Reputation: 9987
The cell value returns a None
when there is no data. It will not return ""
. So, the condition is NOT satisfied and the while loop goes on till the last row of excel. Change...
while ws1.cell(row=i, column=x+2).value != "":
to
while ws1.cell(row=i, column=x+2).value is not None:
...and the code would run as expected.
Upvotes: 1