forked from staale/py-xlsx
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed version, added readme file, modified the tests and included a…
… fixture, date supports different formats
- Loading branch information
Alexandru Plugaru
committed
Jan 20, 2011
1 parent
a3dd65e
commit b89a58e
Showing
7 changed files
with
112 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
python-xlsx | ||
=========== | ||
|
||
A small footprint xslx reader that understands shared strings and can process | ||
excel dates. | ||
|
||
Usage | ||
+++++++ | ||
|
||
:: | ||
|
||
book = Workbook('filename or filedescriptor') #Open xlsx file | ||
for sheet in book: | ||
print sheet.name | ||
for row, cells in sheet.rows().iteritems(): # or sheet.cols() | ||
print row # prints row number | ||
for cell in cells: | ||
print cell.id, cell.value, cell.formula | ||
|
||
# or you can access the sheets by their name: | ||
|
||
some_sheet = book['some sheet name'] | ||
... | ||
|
||
Alternatives | ||
------------ | ||
|
||
To my knowledge there are other python alternatives: | ||
|
||
* https://bitbucket.org/ericgazoni/openpyxl/ | ||
* https://github.com/leegao/pyXLSX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# -*- coding: utf-8 -*- | ||
import os | ||
import unittest | ||
|
||
from xlsx import Workbook | ||
|
||
class WorkbookTestCase(unittest.TestCase): | ||
|
||
def setUp(self): | ||
""" Getting all file from fixtures dir """ | ||
self.workbooks = {} | ||
fixtures_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), | ||
'fixtures')) | ||
xlsx_files = os.listdir(fixtures_dir) | ||
for filename in xlsx_files: | ||
self.workbooks[filename] = Workbook( | ||
os.path.join(fixtures_dir, filename)) | ||
|
||
def test_basic(self): | ||
""" These test will run for all test files """ | ||
|
||
for filename, workbook in self.workbooks.items(): | ||
for sheet in workbook: | ||
assert hasattr(sheet, 'id') | ||
assert isinstance(sheet.name, basestring) | ||
assert isinstance(sheet.rows(), dict) | ||
assert isinstance(sheet.cols(), dict) | ||
|
||
for row_num, cells in sheet.rows().iteritems(): | ||
assert isinstance(row_num, int) | ||
assert isinstance(cells, list) | ||
for cell in cells: | ||
assert hasattr(cell, 'id') | ||
assert hasattr(cell, 'column') | ||
assert hasattr(cell, 'row') | ||
assert hasattr(cell, 'value') | ||
assert cell.row == row_num | ||
|
||
def test_test1(self): | ||
""" Specific test for `testdata/test1.xslx` file including | ||
unicode strings and different date formats | ||
""" | ||
workbook = self.workbooks['test1.xlsx'] | ||
|
||
self.assertEqual(unicode(workbook[1].name), u'рускии') | ||
self.assertEqual(unicode(workbook[2].name), u'性 文化交流 例如') | ||
self.assertEqual(unicode(workbook[3].name), u'تعد. بحق طائ') | ||
|
||
for row_num, cells in workbook[1].rows().iteritems(): | ||
if row_num == 1: | ||
self.assertEqual(unicode(cells[0].value), u'лорем ипсум') | ||
self.assertEqual(unicode(cells[1].value), u'2') | ||
if row_num == 2: #Test date fields | ||
self.assertEqual(cells[0].value, (2010, 11, 12, 0, 0, 0)) | ||
self.assertEqual(cells[1].value, (1987, 12, 20, 0, 0, 0)) | ||
self.assertEqual(cells[2].value, (1987, 12, 20, 0, 0, 0)) | ||
self.assertEqual(cells[3].value, (1987, 12, 20, 0, 0, 0)) | ||
break | ||
|
||
# Cell A1 in u'性 文化交流 例如' | ||
self.assertEqual(unicode(workbook[2].cols()['A'][0].value), | ||
u'性 文化交流 例如') | ||
self.assertEqual(unicode(workbook[2].cols()['A'][1].value), | ||
u'エム セシビ め「こを バジョン') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |