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)
Last active
January 18, 2026 16:33
-
-
Save devinou971/ec137f35b40ad9f96681315839efe25b to your computer and use it in GitHub Desktop.
Excel named table to DataFrame
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment