load_spreadsheets

pyhelpers.store.load_spreadsheets(path_to_file, as_dict=True, verbose=False, **kwargs)[source]

Load multiple sheets of an Microsoft Excel or an OpenDocument format file.

Parameters:
  • path_to_file (str | os.PathLike) – path where a spreadsheet is saved

  • as_dict (bool) – whether to return the retrieved data as a dictionary type, defaults to True

  • verbose (bool | int) – whether to print relevant information in console, defaults to False

  • kwargs – [optional] parameters of pandas.ExcelFile.parse

Returns:

all worksheet in an Excel workbook from the specified file path path_to_spreadsheet

Return type:

list | dict

Note

Examples:

>>> from pyhelpers.store import load_spreadsheets
>>> from pyhelpers.dirs import cd

>>> dat_dir = cd("tests\data")

>>> path_to_xlsx = cd(dat_dir, "dat.ods")
>>> wb_data = load_spreadsheets(path_to_xlsx, verbose=True, index_col=0)
Loading "tests\data\dat.ods" ...
    'TestSheet1'. ... Done.
    'TestSheet2'. ... Done.
>>> list(wb_data.keys())
['TestSheet1', 'TestSheet2']
>>> wb_data['TestSheet1']
            Longitude   Latitude
City
London      -0.127647  51.507322
Birmingham  -1.902691  52.479699
Manchester  -2.245115  53.479489
Leeds       -1.543794  53.797418

>>> path_to_xlsx = cd(dat_dir, "dat.xlsx")
>>> wb_data = load_spreadsheets(path_to_xlsx, verbose=True, index_col=0)
Loading "tests\data\dat.xlsx" ...
    'TestSheet1'. ... Done.
    'TestSheet2'. ... Done.
    'TestSheet11'. ... Done.
    'TestSheet21'. ... Done.
    'TestSheet12'. ... Done.
    'TestSheet22'. ... Done.
>>> list(wb_data.keys())
['TestSheet1',
 'TestSheet2',
 'TestSheet11',
 'TestSheet21',
 'TestSheet12',
 'TestSheet22']

>>> wb_data = load_spreadsheets(path_to_xlsx, as_dict=False, index_col=0)
>>> type(wb_data)
list
>>> len(wb_data)
6
>>> wb_data[0]
            Longitude   Latitude
City
London      -0.127647  51.507322
Birmingham  -1.902691  52.479699
Manchester  -2.245115  53.479489
Leeds       -1.543794  53.797418