Skip to content

Excel Reader

A class to read and extract data from Excel files (.xlsx, .xlsm, .xlsb).

This class provides methods to load workbooks, extract data from sheets, tables, and named ranges, with robust error handling.

__init__(file_path)

Initialize the ExcelReader with the path to the Excel file.

Parameters:

Name Type Description Default
file_path Path | str

Path to the Excel file, specified as a Path object or string.

required

Raises:

Type Description
ImportError

If the file does not exist, is not a valid Excel file.

extract_data(source_name, source_type)

Extract data from an Excel file based on the source type and name.

Expected the specified source type to be either: - An entire worksheet with the header in the first row (sheet) - A named range (range) - An Excel table (table)

Parameters:

Name Type Description Default
source_name str

Name of the sheet, table, or range to extract.

required
source_type str

Type of source ('sheet', 'table', or 'range').

required

Returns:

Type Description
list[dict]

Data extracted as a list of dictionaries.

Raises:

Type Description
ImportError

If the source type is invalid or if there are issues extracting data.

extract_from_range(range_name)

Extract a named range from an Excel file and return it as a list of dictionaries.

Parameters:

Name Type Description Default
range_name str

Name of the range to extract.

required

Returns:

Type Description
list[dict]

A list containing the range data as dictionaries.

Raises:

Type Description
ImportError

If the range cannot be loaded or if there are issues with the file.

extract_from_sheet(sheet_name)

Extract a sheet from an Excel file and return it as a list of dictionaries.

Parameters:

Name Type Description Default
sheet_name str

Name of the sheet to extract.

required

Returns:

Type Description
list[dict]

A list containing the sheet data as dictionaries.

Raises:

Type Description
ImportError

If the sheet cannot be loaded or if there are issues with the file.

extract_from_table(table_name)

Extract a table from an Excel file and return it as a list of dictionaries.

Parameters:

Name Type Description Default
table_name str

Name of the table to extract.

required

Returns:

Type Description
list[dict]

A list containing the table data as dictionaries.

Raises:

Type Description
ImportError

If the table cannot be loaded or if there are issues with the file.

load_excel_workbook(*, data_only=True, read_only=False)

Load an Excel workbook with robust error handling.

Parameters:

Name Type Description Default
data_only bool

Whether to return cell values (not formulas).

True
read_only bool

Use openpyxl's read-only mode for large files.

False

Returns:

Name Type Description
workbook Workbook

Workbook object.

Raises:

Type Description
ImportError

If the file cannot be loaded due to various reasons.