bw2io.extractors.excel#

Classes#

ExcelExtractor

A class used to extract data from an Excel file.

Functions#

get_cell_value_handle_error(cell)

Retrieve the value of a given cell and handle error types.

Module Contents#

class bw2io.extractors.excel.ExcelExtractor[source]#

A class used to extract data from an Excel file.

Parameters:

object (type) – The parent object for the ExcelExtractor class.

Returns:

An instance of the class.

Return type:

object

See also

openpyxl.load_workbook

Load a workbook from a file.

Notes

This class requires the openpyxl package to be installed.

Raises:

AssertionError – If the file at ‘filepath’ does not exist.

Parameters:

filepath (str) – The path to the Excel file.

Returns:

A list of tuples containing the name of each sheet in the file and the data from each sheet.

Return type:

list

Examples

>>> extractor = ExcelExtractor()
>>> filepath = 'example.xlsx'
>>> data = extractor.extract(filepath)
classmethod extract(filepath: pathlib.Path, **kwargs)[source]#

Extract data from an Excel file.

Parameters:

filepath (str) – The path to the Excel file.

Returns:

A list of tuples containing the name of each sheet in the file and the data from each sheet.

Return type:

list

Raises:

AssertionError – If the file at ‘filepath’ does not exist.

classmethod extract_sheet(wb: openpyxl.workbook.Workbook, name: str, strip: bool = True)[source]#

Extract data from a single sheet in an Excel workbook.

Parameters:
  • wb (openpyxl.workbook.Workbook) – The workbook object with the sheet to extract data from.

  • name (str) – The name of the sheet to extract data from.

  • strip (bool, optional) – If True, strip whitespace from cell values, by default True.

Returns:

A list of lists containing the data from the sheet.

Return type:

list

Notes

This method is called by the ‘extract’ method to extract the data from each sheet in the workbook.

Examples

>>> wb = openpyxl.load_workbook('example.xlsx')
>>> name = 'Sheet1'
>>> data = ExcelExtractor.extract_sheet(wb, sheetname)
bw2io.extractors.excel.get_cell_value_handle_error(cell: get_cell_value_handle_error.cell)[source]#

Retrieve the value of a given cell and handle error types.

Parameters:

cell (openpyxl.cell.cell.Cell) – The cell to get the value from.

Returns:

The value of the cell, or None if the cell has an error type.

Return type:

object

Examples

>>> from openpyxl import Workbook
>>> wb = Workbook()
>>> ws = wb.active
>>> ws["A1"] = "hello"
>>> assert get_cell_value_handle_error(ws["A1"]) == "hello"
>>> ws["B1"] = "=1/0"
>>> assert get_cell_value_handle_error(ws["B1"]) == None