diff --git a/frictionless/formats/excel/parsers/__spec__/test_xlsx.py b/frictionless/formats/excel/parsers/__spec__/test_xlsx.py index 0f250c1dc5..4edc72f1c4 100644 --- a/frictionless/formats/excel/parsers/__spec__/test_xlsx.py +++ b/frictionless/formats/excel/parsers/__spec__/test_xlsx.py @@ -67,6 +67,16 @@ def test_xlsx_parser_format_error_sheet_by_index_not_existent(): assert error.note == 'Excel document "data/sheet2.xlsx" does not have a sheet "3"' +def test_xlsx_parser_missing_extra_dependency(monkeypatch: pytest.MonkeyPatch): + # https://github.com/frictionlessdata/frictionless-py/issues/1756 + monkeypatch.setitem(sys.modules, "openpyxl", None) + monkeypatch.delitem(platform.__dict__, "openpyxl", raising=False) + resource = TableResource(path="data/table.xlsx") + with pytest.raises(FrictionlessException) as excinfo: + resource.open() + assert excinfo.value.error.note == 'Please install "frictionless[excel]"' + + def test_xlsx_parser_sheet_by_name(): path = "data/sheet2.xlsx" control = formats.ExcelControl(sheet="Sheet2") diff --git a/frictionless/formats/excel/parsers/xlsx.py b/frictionless/formats/excel/parsers/xlsx.py index 591afb79e1..a09843f2d3 100644 --- a/frictionless/formats/excel/parsers/xlsx.py +++ b/frictionless/formats/excel/parsers/xlsx.py @@ -79,9 +79,12 @@ def read_cell_stream_create(self): # Get book # To fill merged cells we can't use read-only because # `sheet.merged_cell_ranges` is not available in this mode + # NOTE: resolve the dependency outside the try/except so that a missing + # `frictionless[excel]` install is not reported as an invalid excel file + openpyxl = platform.openpyxl try: warnings.filterwarnings("ignore", category=UserWarning, module="openpyxl") - book = platform.openpyxl.load_workbook( + book = openpyxl.load_workbook( self.loader.byte_stream, read_only=not control.fill_merged_cells, data_only=True,