ValueError: Row numbers must be between 1 and 1048576

Question:

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?

Asked By: PulaNegro

||

Answers:

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.

Answered By: Redox

Lo que pasa es que en algún momento el valor de la columna está siendo 0 y error dice que los numeros deben estar entre 1 y 1048576

Answered By: Jhoan Peña
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.