Skip to content

Commit 4244598

Browse files
committed
🐛 fix #18, convert the non-seekable io instance to a seekable one because python's ZipFile would rewind itself at some point
1 parent 125d41d commit 4244598

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pyexcel_xlsx/xlsxr.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
:license: New BSD License
99
"""
1010
import openpyxl
11+
from io import UnsupportedOperation
1112

1213
from pyexcel_io.book import BookReader
1314
from pyexcel_io.sheet import SheetReader
14-
from pyexcel_io._compact import OrderedDict
15+
from pyexcel_io._compact import OrderedDict, BytesIO
1516

1617

1718
class XLSXSheet(SheetReader):
@@ -49,6 +50,16 @@ def open(self, file_name, skip_hidden_sheets=True, **keywords):
4950
self._load_the_excel_file(file_name)
5051

5152
def open_stream(self, file_stream, skip_hidden_sheets=True, **keywords):
53+
if not hasattr(file_stream, 'seek'):
54+
# python 2
55+
# Hei zipfile in odfpy would do a seek
56+
# but stream from urlib cannot do seek
57+
file_stream = BytesIO(file_stream.read())
58+
try:
59+
file_stream.seek(0)
60+
except UnsupportedOperation:
61+
# python 3
62+
file_stream = BytesIO(file_stream.read())
5263
BookReader.open_stream(self, file_stream, **keywords)
5364
self.skip_hidden_sheets = skip_hidden_sheets
5465
self._load_the_excel_file(file_stream)

tests/test_bug_fixes.py

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
from textwrap import dedent
1010
import pyexcel as pe
1111
from nose.tools import eq_
12+
from nose import SkipTest
13+
14+
IN_TRAVIS = 'TRAVIS' in os.environ
1215

1316

1417
PY36_ABOVE = sys.version_info[0] == 3 and sys.version_info[1] >= 6
@@ -97,5 +100,11 @@ def test_issue_8_hidden_sheet_2():
97100
eq_(book_dict['hidden'], [['a', 'b']])
98101

99102

103+
def test_issue_20():
104+
#if not IN_TRAVIS:
105+
# raise SkipTest()
106+
pe.get_book(url="https://github.com/pyexcel/pyexcel-xlsx/raw/master/tests/fixtures/file_with_an_empty_sheet.xlsx"); # flake8: noqa
107+
108+
100109
def get_fixtures(file_name):
101110
return os.path.join("tests", "fixtures", file_name)

0 commit comments

Comments
 (0)