Skip to content

Commit 7da951f

Browse files
ftruzzigfyoung
authored andcommitted
ENH: add support for reading binary Excel files (#30519)
Closes gh-15914
1 parent dd0d353 commit 7da951f

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,7 @@ I/O
931931
- Bug in :class:`PythonParser` where str and bytes were being mixed when dealing with the decimal field (:issue:`29650`)
932932
- :meth:`read_gbq` now accepts ``progress_bar_type`` to display progress bar while the data downloads. (:issue:`29857`)
933933
- Bug in :func:`pandas.io.json.json_normalize` where a missing value in the location specified by `record_path` would raise a ``TypeError`` (:issue:`30148`)
934+
- :func:`read_excel` now accepts binary data (:issue:`15914`)
934935

935936
Plotting
936937
^^^^^^^^

pandas/io/excel/_base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
4141
Parameters
4242
----------
43-
io : str, ExcelFile, xlrd.Book, path object or file-like object
43+
io : str, bytes, ExcelFile, xlrd.Book, path object, or file-like object
4444
Any valid string path is acceptable. The string could be a URL. Valid
4545
URL schemes include http, ftp, s3, and file. For file URLs, a host is
4646
expected. A local file could be: ``file://localhost/path/to/table.xlsx``.
@@ -350,6 +350,8 @@ def __init__(self, filepath_or_buffer):
350350
self.book = self.load_workbook(filepath_or_buffer)
351351
elif isinstance(filepath_or_buffer, str):
352352
self.book = self.load_workbook(filepath_or_buffer)
353+
elif isinstance(filepath_or_buffer, bytes):
354+
self.book = self.load_workbook(BytesIO(filepath_or_buffer))
353355
else:
354356
raise ValueError(
355357
"Must explicitly set engine if not passing in buffer or path for io."

pandas/tests/io/excel/test_readers.py

+10
Original file line numberDiff line numberDiff line change
@@ -988,3 +988,13 @@ def test_conflicting_excel_engines(self, read_ext):
988988
with pd.ExcelFile("test1" + read_ext) as xl:
989989
with pytest.raises(ValueError, match=msg):
990990
pd.read_excel(xl, engine="foo")
991+
992+
def test_excel_read_binary(self, engine, read_ext):
993+
# GH 15914
994+
expected = pd.read_excel("test1" + read_ext, engine=engine)
995+
996+
with open("test1" + read_ext, "rb") as f:
997+
data = f.read()
998+
999+
actual = pd.read_excel(data, engine=engine)
1000+
tm.assert_frame_equal(expected, actual)

0 commit comments

Comments
 (0)