Find range of filled contents in Excel worksheet

2019-08-22 08:48发布

问题:

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?

回答1:

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

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