Skip to content

Commit e796d73

Browse files
committed
Optimization: support loading only one sheet from many in order to save memory usage
1 parent 774a7cb commit e796d73

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

pyexcel_xls/__init__.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,38 @@ class XLBook(BookReader):
103103
104104
It reads xls, xlsm, xlsx work book
105105
"""
106+
def __init__(self, filename, file_content=None, sheetname=None, **keywords):
107+
BookReader.__init__(self, filename, file_content=file_content, sheetname=sheetname, **keywords)
108+
self.native_book.release_resources()
109+
106110
def sheetIterator(self):
107111
"""Return iterable sheet array"""
108-
return self.native_book.sheets()
112+
113+
if self.sheet_name is not None:
114+
return [self.native_book.sheet_by_name(self.sheet_name)]
115+
elif self.sheet_index is not None:
116+
return [self.native_book.sheet_by_index(self.sheet_index)]
117+
else:
118+
return self.native_book.sheets()
109119

110120
def getSheet(self, native_sheet):
111121
"""Create a xls sheet"""
112122
return XLSheet(native_sheet)
113123

114-
def load_from_memory(self, file_content):
124+
def load_from_memory(self, file_content, **keywords):
115125
"""Provide the way to load xls from memory"""
116-
return xlrd.open_workbook(None, file_contents=file_content)
126+
on_demand = False
127+
if self.sheet_name is not None or self.sheet_index is not None:
128+
on_demand = True
129+
return xlrd.open_workbook(None, file_contents=file_content,
130+
on_demand=on_demand)
117131

118-
def load_from_file(self, filename):
132+
def load_from_file(self, filename, **keywords):
119133
"""Provide the way to load xls from a file"""
120-
return xlrd.open_workbook(filename)
134+
on_demand = False
135+
if self.sheet_name is not None or self.sheet_index is not None:
136+
on_demand = True
137+
return xlrd.open_workbook(filename, on_demand=on_demand)
121138

122139

123140
class XLSheetWriter(SheetWriter):
@@ -198,4 +215,4 @@ def close(self):
198215
# to allow this module to function independently
199216
pass
200217

201-
__VERSION__ = "0.0.5"
218+
__VERSION__ = "0.0.6"

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
dependencies = [
1313
'xlrd',
1414
'xlwt-future',
15-
'pyexcel-io>=0.0.2'
15+
'pyexcel-io>=0.0.3'
1616
]
1717
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
1818
dependencies.append('ordereddict')
1919

2020
setup(
2121
name='pyexcel-xls',
2222
author="C. W.",
23-
version='0.0.5',
23+
version='0.0.6',
2424
author_email="[email protected]",
2525
url="https://github.com/chfw/pyexcel-xls",
2626
description='A wrapper library to read, manipulate and write data in xls format. It reads xlsx and xlsm format',

tests/test_mutliple_sheets.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,6 @@ def tearDown(self):
1919
self._clean_up()
2020

2121

22-
#class TestXlsNXlsxMultipleSheets(PyexcelMultipleSheetBase):
23-
# def setUp(self):
24-
# self.testfile = "multiple1.xls"
25-
# self.testfile2 = "multiple1.xlsx"
26-
# self.content = {
27-
# "Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]],
28-
# "Sheet2": [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]],
29-
# "Sheet3": [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]
30-
# }
31-
# self._write_test_file(self.testfile)
32-
#
33-
# def tearDown(self):
34-
# self._clean_up()
35-
36-
3722
class TestAddBooks:
3823
def _write_test_file(self, file):
3924
"""
@@ -60,6 +45,16 @@ def setUp(self):
6045
self._write_test_file(self.testfile)
6146
self._write_test_file(self.testfile2)
6247

48+
def test_load_a_single_sheet(self):
49+
b1 = pyexcel.load_book(self.testfile, sheet_name="Sheet1")
50+
assert len(b1.sheet_names()) == 1
51+
assert b1['Sheet1'].to_array() == self.content['Sheet1']
52+
53+
def test_load_a_single_sheet2(self):
54+
b1 = pyexcel.load_book(self.testfile, sheet_index=0)
55+
assert len(b1.sheet_names()) == 1
56+
assert b1['Sheet1'].to_array() == self.content['Sheet1']
57+
6358
def test_delete_sheets(self):
6459
b1 = pyexcel.load_book(self.testfile)
6560
assert len(b1.sheet_names()) == 3

0 commit comments

Comments
 (0)