Home > Software design >  Find the coordinate of the first value in a worksheet [openpyxl]
Find the coordinate of the first value in a worksheet [openpyxl]

Time:05-26

In a workbook At any active worksheet. how can I find the coordinate of the first cell with any kind of value in it?. And also for the last cell

Example:

At cell "D15" Its a table made of 15 columns (right) and 50 rows (down)

Is there a way to get..

Start "D15" End "S66" --> range "D15:S66"

CodePudding user response:

Within openpyxl, you have min_row, min_column, max_row and max_column. Assuming you have some form of a table in range D15:S66, the values will be min_column = 4 for 'D', min_row = 15, max_column=19 for 'S' and max_row=66. Sample code is here.

from openpyxl import load_workbook
data_file='test.xlsx'
wb = load_workbook(data_file)
ws = wb.worksheets[0]

print(ws.min_row, ws.min_column, ws.cell(ws.min_row,ws.min_column).value)
print(ws.max_row, ws.max_column, ws.cell(ws.max_row,ws.max_column).value)

Output

15 4 Header1
66 19 33333
  • Related