user1315789
user1315789

Reputation: 3659

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?

Upvotes: 3

Views: 2917

Answers (2)

mouwsy
mouwsy

Reputation: 1933

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)

Upvotes: 4

Vityata
Vityata

Reputation: 43585

If wb is defined as Excel Workbook, then this is a good way:

print (wb.sheets[sheet_name].api.UsedRange.Address)

Upvotes: 1

Related Questions