Skip to content

Instantly share code, notes, and snippets.

@devinou971
Last active January 18, 2026 16:33
Show Gist options
  • Select an option

  • Save devinou971/ec137f35b40ad9f96681315839efe25b to your computer and use it in GitHub Desktop.

Select an option

Save devinou971/ec137f35b40ad9f96681315839efe25b to your computer and use it in GitHub Desktop.
Excel named table to DataFrame

Excel named table to DataFrame

from openpyxl.worksheet.table import Table
from openpyxl.worksheet.worksheet import Worksheet
import pandas as pd
from openpyxl.utils.cell import range_boundaries

def table_to_dataframe(ws : Worksheet, table_name : str) -> pd.DataFrame:
    if table_name not in ws.tables.keys():
        raise Exception(f"{table_name} doesn't exist in the worksheet")
    
    table : Table = ws.tables[table_name]
    headers = table.column_names

    first_col, first_row, last_col, last_row = range_boundaries(table.ref)
    first_row += table.headerRowCount or 0
    last_row -= table.totalsRowCount or 0

    data_rows_iter = ws.iter_rows(first_row, last_row, first_col, last_col, values_only=True)

    return pd.DataFrame(data_rows_iter, columns=headers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment