Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find range of filled contents in Excel worksheet

I have an Excel 2016 Book.xlsm. In the worksheet testsheet, the cells in the range A1:Y150 are filled with text or number contents. The upper-left cell is always A1.

I am using python v3 xlwings to open the Excel file.

import xlwings as xw
Book_name = 'C:/Users/name/Book.xlsm'
sheet_name = 'testsheet'
wb = xw.Book(Book_name)
sht = wb.sheets[sheet_name]

How do I find out the range of cells that are filled with contents using python, which in this case is A1:Y150?

like image 723
user1315789 Avatar asked Dec 06 '25 07:12

user1315789


1 Answers

You can get the range filled with contents with used_range:

import xlwings as xw

filename = "test.xlsx"

wb = xw.Book(filename)
ws = wb.sheets["SheetX"]
a_range = ws.used_range.address
print(a_range)
like image 118
mouwsy Avatar answered Dec 07 '25 20:12

mouwsy