Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xlwings function to find the last row with data

I am trying to find the last row in a column with data. to replace the vba function: LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

I am trying this, but this pulls in all rows in Excel. How can I just get the last row.

from xlwings import Workbook, Range
wb = Workbook()
print len(Range('A:A'))
like image 422
user2242044 Avatar asked Oct 29 '15 15:10

user2242044


People also ask

How do I find the last row of my Xlwings?

You have to go through every column to determine which row is the last row. I added lastRow and lastColumn. To make the program more effective, you can set these parameters according to the approximate shape of the data you're dealing with.

How do I select data to the last row?

1. Select the Last Used Cell. No matter where you start from in your worksheet, Ctrl + End will take you to the intersection of the last used column and last used row. Sometimes, when you use this shortcut, Excel will move your selection so that is farther to the right or farther down than the data range you can see.

What can you do with Xlwings?

Xlwings can be used to insert data in an Excel file similarly that it reads from an Excel file. Data can be provided as a list or a single input to a certain cell or a selection of cells.


1 Answers

We can use Range object to find the last row and/or the last column:

import xlwings as xw

# open raw data file
filename_read = 'data_raw.csv'
wb = xw.Book(filename_read)
sht = wb.sheets[0]

# find the numbers of columns and rows in the sheet
num_col = sht.range('A1').end('right').column
num_row = sht.range('A1').end('down').row

# collect data
content_list = sht.range((1,1),(num_row,num_col)).value
print(content_list)
like image 69
Sung Eun Jo Avatar answered Sep 18 '22 23:09

Sung Eun Jo