Skip to content

Commit 67cba78

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

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

pandas/io/excel/_base.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,10 @@ def read_excel(
404404

405405
class BaseExcelReader(metaclass=abc.ABCMeta):
406406
def __init__(self, filepath_or_buffer, storage_options: StorageOptions = None):
407-
self.handles = IOHandles(
408-
handle=filepath_or_buffer, compression={"method": None}
409-
)
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+
410411
if not isinstance(filepath_or_buffer, (ExcelFile, self._workbook_class)):
411412
self.handles = get_handle(
412413
filepath_or_buffer, "rb", storage_options=storage_options, is_text=False
@@ -422,8 +423,6 @@ def __init__(self, filepath_or_buffer, storage_options: StorageOptions = None):
422423
except Exception:
423424
self.close()
424425
raise
425-
elif isinstance(self.handles.handle, bytes):
426-
self.book = self.load_workbook(BytesIO(self.handles.handle))
427426
else:
428427
raise ValueError(
429428
"Must explicitly set engine if not passing in buffer or path for io."
@@ -1111,7 +1110,7 @@ class ExcelFile:
11111110
11121111
Parameters
11131112
----------
1114-
path_or_buffer : str, path object (pathlib.Path or py._path.local.LocalPath),
1113+
path_or_buffer : str, bytes, path object (pathlib.Path or py._path.local.LocalPath),
11151114
a file-like object, xlrd workbook or openpyxl workbook.
11161115
If a string or path object, expected to be a path to a
11171116
.xls, .xlsx, .xlsb, .xlsm, .odf, .ods, or .odt file.
@@ -1170,6 +1169,10 @@ def __init__(
11701169
if engine is not None and engine not in self._engines:
11711170
raise ValueError(f"Unknown engine: {engine}")
11721171

1172+
# First argument can also be bytes, so create a buffer
1173+
if isinstance(path_or_buffer, bytes):
1174+
path_or_buffer = BytesIO(path_or_buffer)
1175+
11731176
# Could be a str, ExcelFile, Book, etc.
11741177
self.io = path_or_buffer
11751178
# Always a string

0 commit comments

Comments
 (0)