Skip to content

Commit 9ed6b4d

Browse files
Add automatic OpenDocument Spreadsheet recognition to ExcelFile class
1 parent 5e74bd1 commit 9ed6b4d

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

pandas/io/excel/_base.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import abc
22
import datetime
3-
from io import BytesIO
3+
from io import BytesIO, IOBase
44
import os
55
from textwrap import fill
66

@@ -778,6 +778,17 @@ def close(self):
778778
return self.save()
779779

780780

781+
def _is_ods_stream(stream):
782+
stream.seek(0)
783+
is_ods = False
784+
if stream.read(4) == b"PK\003\004":
785+
stream.seek(30)
786+
is_ods = stream.read(54) == b"mimetype" \
787+
b"application/vnd.oasis.opendocument.spreadsheet"
788+
stream.seek(0)
789+
return is_ods
790+
791+
781792
class ExcelFile:
782793
"""
783794
Class for parsing tabular excel sheets into DataFrame objects.
@@ -809,9 +820,12 @@ class ExcelFile:
809820
def __init__(self, path_or_io, engine=None):
810821
if engine is None:
811822
engine = "xlrd"
812-
if isinstance(path_or_io, str):
813-
ext = os.path.splitext(path_or_io)[-1][1:]
814-
if ext == "ods":
823+
if isinstance(path_or_io, IOBase):
824+
if _is_ods_stream(path_or_io):
825+
engine = "odf"
826+
else:
827+
ext = os.path.splitext(str(path_or_io))[-1]
828+
if ext == ".ods":
815829
engine = "odf"
816830
if engine not in self._engines:
817831
raise ValueError(f"Unknown engine: {engine}")

0 commit comments

Comments
 (0)