Skip to content

REGR: do not try to infer the excel type from a workbook object #39587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Fixed regressions
- Fixed regression in :meth:`~DataFrame.to_pickle` failing to create bz2/xz compressed pickle files with ``protocol=5`` (:issue:`39002`)
- Fixed regression in :func:`pandas.testing.assert_series_equal` and :func:`pandas.testing.assert_frame_equal` always raising ``AssertionError`` when comparing extension dtypes (:issue:`39410`)
- Fixed regression in :meth:`~DataFrame.to_csv` opening ``codecs.StreamWriter`` in binary mode instead of in text mode and ignoring user-provided ``mode`` (:issue:`39247`)
- Fixed regression in :func:`read_excel` that caused it to raise a ``ValueError`` when mistakenly treating a workbook object as a file handle (:issue:`39528`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ def __init__(

if xlrd_version is not None and isinstance(path_or_buffer, xlrd.Book):
ext = "xls"
else:
elif engine in (None, "xlrd") and True:
ext = inspect_excel_format(
content_or_path=path_or_buffer, storage_options=storage_options
)
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/io/excel/test_openpyxl.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,27 @@ def test_to_excel_with_openpyxl_engine(ext):
).highlight_max()

styled.to_excel(filename, engine="openpyxl")


@pytest.mark.parametrize("engine", (None, "openpyxl"))
def test_read_excel_workbook(engine, ext):
# GH 39528
openpyxl = pytest.importorskip("openpyxl")
expected = tm.makeDataFrame()
message = "Invalid file path or buffer object type"

with tm.ensure_clean(ext) as filename:
expected.to_excel(filename)
workbook = openpyxl.load_workbook(filename)
if engine is None:
# should raise
try:
with pytest.raises(ValueError, match=message):
df = pd.read_excel(workbook, engine=engine)
finally:
workbook.close()
else:
# should work
df = pd.read_excel(workbook, engine=engine, index_col=0)
workbook.close()
tm.assert_frame_equal(expected, df)