Skip to content

Commit 5f99a9b

Browse files
committed
FIX: bytes -> BytesIO buffer in __init__ for BaseExcelReader & ExcelFile
1 parent 4f019ad commit 5f99a9b

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

pandas/io/excel/_base.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,10 @@ def read_excel(
404404

405405
class BaseExcelReader(metaclass=abc.ABCMeta):
406406
def __init__(self, filepath_or_buffer, storage_options: StorageOptions = None):
407+
# First argument can also be bytes, so create a buffer
408+
if isinstance(filepath_or_buffer, bytes):
409+
filepath_or_buffer = BytesIO(filepath_or_buffer)
410+
407411
self.handles = IOHandles(
408412
handle=filepath_or_buffer, compression={"method": None}
409413
)
@@ -422,8 +426,6 @@ def __init__(self, filepath_or_buffer, storage_options: StorageOptions = None):
422426
except Exception:
423427
self.close()
424428
raise
425-
elif isinstance(self.handles.handle, bytes):
426-
self.book = self.load_workbook(BytesIO(self.handles.handle))
427429
else:
428430
raise ValueError(
429431
"Must explicitly set engine if not passing in buffer or path for io."
@@ -1111,7 +1113,7 @@ class ExcelFile:
11111113
11121114
Parameters
11131115
----------
1114-
path_or_buffer : str, path object (pathlib.Path or py._path.local.LocalPath),
1116+
path_or_buffer : str, bytes, path object (pathlib.Path or py._path.local.LocalPath),
11151117
a file-like object, xlrd workbook or openpyxl workbook.
11161118
If a string or path object, expected to be a path to a
11171119
.xls, .xlsx, .xlsb, .xlsm, .odf, .ods, or .odt file.
@@ -1170,6 +1172,10 @@ def __init__(
11701172
if engine is not None and engine not in self._engines:
11711173
raise ValueError(f"Unknown engine: {engine}")
11721174

1175+
# First argument can also be bytes, so create a buffer
1176+
if isinstance(path_or_buffer, bytes):
1177+
path_or_buffer = BytesIO(path_or_buffer)
1178+
11731179
# Could be a str, ExcelFile, Book, etc.
11741180
self.io = path_or_buffer
11751181
# Always a string

0 commit comments

Comments
 (0)