Skip to content
Advertisement

How to scroll down to last row with data?

I have an Excel file with many rows and it gets many entries daily. That’s why I would like to scroll down the file to last row with data.

To find the last row, I use:

last_row = workbook.sheets[0].range('A1').end('down').row 

How could I scroll down to it using Python (and preferably XLWings module)?

Advertisement

Answer

Use used_range, it’s more robust than expand("down) or end("down"), if you have empty cells in the table. Here is the code:

import xlwings as xw

path = r"test.xlsx"

wb = xw.Book(path)
ws = wb.sheets[0]
ws.used_range[-1:,:].select()

I have used an index on the range object [-1:,:], the numbers before the comma represent the start range cell, the numbers after the comma represent the end range cell. If you would like to select the single cell of the last row in the first column used_range[-1:,:][0] would do it.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement