Optimization: support loading only one sheet from many in order to save memory usage

This commit is contained in:
chfw 2015-02-17 23:11:17 +00:00
parent 774a7cb07d
commit e796d73eca
3 changed files with 35 additions and 23 deletions

View File

@ -103,21 +103,38 @@ class XLBook(BookReader):
It reads xls, xlsm, xlsx work book
"""
def __init__(self, filename, file_content=None, sheetname=None, **keywords):
BookReader.__init__(self, filename, file_content=file_content, sheetname=sheetname, **keywords)
self.native_book.release_resources()
def sheetIterator(self):
"""Return iterable sheet array"""
return self.native_book.sheets()
if self.sheet_name is not None:
return [self.native_book.sheet_by_name(self.sheet_name)]
elif self.sheet_index is not None:
return [self.native_book.sheet_by_index(self.sheet_index)]
else:
return self.native_book.sheets()
def getSheet(self, native_sheet):
"""Create a xls sheet"""
return XLSheet(native_sheet)
def load_from_memory(self, file_content):
def load_from_memory(self, file_content, **keywords):
"""Provide the way to load xls from memory"""
return xlrd.open_workbook(None, file_contents=file_content)
on_demand = False
if self.sheet_name is not None or self.sheet_index is not None:
on_demand = True
return xlrd.open_workbook(None, file_contents=file_content,
on_demand=on_demand)
def load_from_file(self, filename):
def load_from_file(self, filename, **keywords):
"""Provide the way to load xls from a file"""
return xlrd.open_workbook(filename)
on_demand = False
if self.sheet_name is not None or self.sheet_index is not None:
on_demand = True
return xlrd.open_workbook(filename, on_demand=on_demand)
class XLSheetWriter(SheetWriter):
@ -198,4 +215,4 @@ except:
# to allow this module to function independently
pass
__VERSION__ = "0.0.5"
__VERSION__ = "0.0.6"

View File

@ -12,7 +12,7 @@ with open("README.rst", 'r') as readme:
dependencies = [
'xlrd',
'xlwt-future',
'pyexcel-io>=0.0.2'
'pyexcel-io>=0.0.3'
]
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
dependencies.append('ordereddict')
@ -20,7 +20,7 @@ if sys.version_info[0] == 2 and sys.version_info[1] < 7:
setup(
name='pyexcel-xls',
author="C. W.",
version='0.0.5',
version='0.0.6',
author_email="wangc_2011@hotmail.com",
url="https://github.com/chfw/pyexcel-xls",
description='A wrapper library to read, manipulate and write data in xls format. It reads xlsx and xlsm format',

View File

@ -19,21 +19,6 @@ class TestXlsNxlsMultipleSheets(PyexcelMultipleSheetBase):
self._clean_up()
#class TestXlsNXlsxMultipleSheets(PyexcelMultipleSheetBase):
# def setUp(self):
# self.testfile = "multiple1.xls"
# self.testfile2 = "multiple1.xlsx"
# self.content = {
# "Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]],
# "Sheet2": [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]],
# "Sheet3": [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]
# }
# self._write_test_file(self.testfile)
#
# def tearDown(self):
# self._clean_up()
class TestAddBooks:
def _write_test_file(self, file):
"""
@ -60,6 +45,16 @@ class TestAddBooks:
self._write_test_file(self.testfile)
self._write_test_file(self.testfile2)
def test_load_a_single_sheet(self):
b1 = pyexcel.load_book(self.testfile, sheet_name="Sheet1")
assert len(b1.sheet_names()) == 1
assert b1['Sheet1'].to_array() == self.content['Sheet1']
def test_load_a_single_sheet2(self):
b1 = pyexcel.load_book(self.testfile, sheet_index=0)
assert len(b1.sheet_names()) == 1
assert b1['Sheet1'].to_array() == self.content['Sheet1']
def test_delete_sheets(self):
b1 = pyexcel.load_book(self.testfile)
assert len(b1.sheet_names()) == 3