test_xlrd.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import io
  2. import pytest
  3. import pandas as pd
  4. import pandas._testing as tm
  5. from pandas.io.excel import ExcelFile
  6. from pandas.io.excel._base import inspect_excel_format
  7. xlrd = pytest.importorskip("xlrd")
  8. exts = [".xls"]
  9. @pytest.fixture(params=exts)
  10. def read_ext_xlrd(request):
  11. """
  12. Valid extensions for reading Excel files with xlrd.
  13. Similar to read_ext, but excludes .ods, .xlsb, and for xlrd>2 .xlsx, .xlsm
  14. """
  15. return request.param
  16. def test_read_xlrd_book(read_ext_xlrd, datapath):
  17. engine = "xlrd"
  18. sheet_name = "Sheet1"
  19. pth = datapath("io", "data", "excel", "test1.xls")
  20. with xlrd.open_workbook(pth) as book:
  21. with ExcelFile(book, engine=engine) as xl:
  22. result = pd.read_excel(xl, sheet_name=sheet_name, index_col=0)
  23. expected = pd.read_excel(
  24. book, sheet_name=sheet_name, engine=engine, index_col=0
  25. )
  26. tm.assert_frame_equal(result, expected)
  27. def test_read_xlsx_fails(datapath):
  28. # GH 29375
  29. from xlrd.biffh import XLRDError
  30. path = datapath("io", "data", "excel", "test1.xlsx")
  31. with pytest.raises(XLRDError, match="Excel xlsx file; not supported"):
  32. pd.read_excel(path, engine="xlrd")
  33. @pytest.mark.parametrize(
  34. "file_header",
  35. [
  36. b"\x09\x00\x04\x00\x07\x00\x10\x00",
  37. b"\x09\x02\x06\x00\x00\x00\x10\x00",
  38. b"\x09\x04\x06\x00\x00\x00\x10\x00",
  39. b"\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1",
  40. ],
  41. )
  42. def test_read_old_xls_files(file_header):
  43. # GH 41226
  44. f = io.BytesIO(file_header)
  45. assert inspect_excel_format(f) == "xls"