python-3.xxlwings

Want to find the table in excel sheet


I want to find the table in excel and assign it into an array.

enter image description here

Consider above excel image an example, First few rows have empty and/or middle rows contains some unwanted metadata or vice versa.

I want to only select the table below from Roll No, Name, etc.) to end of the table and assign it into an array temp = [].

How can it be achieved?

Also sometimes, there won't be any empty row cells, then how can we get the table Is there anyway we can use "Roll No" to identify the table? enter image description here


Solution

  • If you would like to select always the cell group at the bottom, filled with data, you can navigate to the concerning range.

    import xlwings as xw
    
    path = r"test.xlsx"
    
    wb = xw.Book(path)
    ws = wb.sheets[0]
    
    ws.used_range[-1:,:].end("up").expand().select()  # Selects the range.
    range_data = ws.used_range[-1:,:].end("up").expand().value # Stores the range as list of lists.
    

    You could also store the data as a pandas dataframe, which might be a more common use case:

    df = ws.used_range[-1:,:].end("up").expand().options(convert=pd.DataFrame).value