1import pytest
2
3from pandas.compat._optional import import_optional_dependency
4
5import pandas as pd
6import pandas._testing as tm
7from pandas.tests.io.excel import xlrd_version
8
9from pandas.io.excel import ExcelFile
10
11xlrd = pytest.importorskip("xlrd")
12xlwt = pytest.importorskip("xlwt")
13
14
15@pytest.fixture(autouse=True)
16def skip_ods_and_xlsb_files(read_ext):
17    if read_ext == ".ods":
18        pytest.skip("Not valid for xlrd")
19    if read_ext == ".xlsb":
20        pytest.skip("Not valid for xlrd")
21    if read_ext in (".xlsx", ".xlsm") and xlrd_version >= "2":
22        pytest.skip("Not valid for xlrd >= 2.0")
23
24
25def test_read_xlrd_book(read_ext, frame):
26    df = frame
27
28    engine = "xlrd"
29    sheet_name = "SheetA"
30
31    with tm.ensure_clean(read_ext) as pth:
32        df.to_excel(pth, sheet_name)
33        book = xlrd.open_workbook(pth)
34
35        with ExcelFile(book, engine=engine) as xl:
36            result = pd.read_excel(xl, sheet_name=sheet_name, index_col=0)
37            tm.assert_frame_equal(df, result)
38
39        result = pd.read_excel(book, sheet_name=sheet_name, engine=engine, index_col=0)
40        tm.assert_frame_equal(df, result)
41
42
43# TODO: test for openpyxl as well
44def test_excel_table_sheet_by_index(datapath, read_ext):
45    path = datapath("io", "data", "excel", f"test1{read_ext}")
46    msg = "Worksheet named 'invalid_sheet_name' not found"
47    with ExcelFile(path, engine="xlrd") as excel:
48        with pytest.raises(ValueError, match=msg):
49            pd.read_excel(excel, sheet_name="invalid_sheet_name")
50
51
52def test_excel_file_warning_with_xlsx_file(datapath):
53    # GH 29375
54    path = datapath("io", "data", "excel", "test1.xlsx")
55    has_openpyxl = (
56        import_optional_dependency(
57            "openpyxl", raise_on_missing=False, on_version="ignore"
58        )
59        is not None
60    )
61    if not has_openpyxl:
62        with tm.assert_produces_warning(
63            FutureWarning,
64            raise_on_extra_warnings=False,
65            match="The xlrd engine is no longer maintained",
66        ):
67            ExcelFile(path, engine=None)
68    else:
69        with tm.assert_produces_warning(None):
70            pd.read_excel(path, "Sheet1", engine=None)
71
72
73def test_read_excel_warning_with_xlsx_file(datapath):
74    # GH 29375
75    path = datapath("io", "data", "excel", "test1.xlsx")
76    has_openpyxl = (
77        import_optional_dependency(
78            "openpyxl", raise_on_missing=False, on_version="ignore"
79        )
80        is not None
81    )
82    if not has_openpyxl:
83        if xlrd_version >= "2":
84            with pytest.raises(
85                ValueError,
86                match="Your version of xlrd is ",
87            ):
88                pd.read_excel(path, "Sheet1", engine=None)
89        else:
90            with tm.assert_produces_warning(
91                FutureWarning,
92                raise_on_extra_warnings=False,
93                match="The xlrd engine is no longer maintained",
94            ):
95                pd.read_excel(path, "Sheet1", engine=None)
96    else:
97        with tm.assert_produces_warning(None):
98            pd.read_excel(path, "Sheet1", engine=None)
99