REGISTER NOW: DdS Autumn School! 🇨🇭 Grosshöchstetten (Switzerland) 🗓️ 6.-11. October 2024

bw2io.extractors.excel#

Module Contents#

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.

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

A class used to extract data from an Excel file.

Parameters#

objecttype

The parent object for the ExcelExtractor class.

Returns#

object

An instance of the class.

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#

filepathstr

The path to the Excel file.

Returns#

list

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

Examples#

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

Extract data from an Excel file.

Parameters#
filepathstr

The path to the Excel file.

Returns#
list

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

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#
wbopenpyxl.workbook.Workbook

The workbook object with the sheet to extract data from.

namestr

The name of the sheet to extract data from.

stripbool, optional

If True, strip whitespace from cell values, by default True.

Returns#
list

A list of lists containing the data from the sheet.

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#

cellopenpyxl.cell.cell.Cell

The cell to get the value from.

Returns#

object

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

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