0

How can I read excel rows from bottom up.

Reading them top to bottom with the following code has no problem at all

for row in range(2, ws.max_row+1):
            print(ws.cell(row, 1).value)

But doing for loop in reverse doesn't print a thing. Is there anything wrong with the below code?

for row in range(ws.max_row, 1):
            print(ws.cell(row, 1).value)
            row -= 1

Appreciate any help, newbie here..

la_mun
  • 5
  • 3
  • 1
    I think you need to use the `step` parameter of `range()`. – quamrana Dec 17 '22 at 15:00
  • tnx...searched and tried the step, and its giving me this error: ValueError("Row or column values must be at least 1")................this is my code now: for row in (ws.max_row, 2, -1): print(ws.cell(row, 1).value) – la_mun Dec 17 '22 at 15:12
  • Please add the new code and the full error traceback to your question. – quamrana Dec 17 '22 at 15:15
  • Sorry, please add this to the text of your question, not in the comments. – quamrana Dec 17 '22 at 15:19
  • @quamrana tnx a lot.. it worked now.. just forgot to include the word 'range'.. sorry for that :).. not sure why my ide did not flag that missing term.. – la_mun Dec 17 '22 at 15:28

0 Answers0