-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
235 additions
and
157 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 |
---|---|---|
|
@@ -5,3 +5,4 @@ ignored-files/ | |
*.sublime-* | ||
.pypirc | ||
dist/ | ||
.tox |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from setuptools import setup | ||
|
||
setup( | ||
version="0.2", | ||
version="0.4", | ||
name='py-xlsx', | ||
description="""Tiny python code for parsing data from Microsoft's Office | ||
Open XML Spreadsheet format""", | ||
|
@@ -13,10 +13,18 @@ | |
'License :: OSI Approved :: MIT License', | ||
'Operating System :: OS Independent', | ||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 2', | ||
'Programming Language :: Python :: 2.6', | ||
'Programming Language :: Python :: 2.7', | ||
'Programming Language :: Python :: 3', | ||
'Programming Language :: Python :: 3.2', | ||
'Programming Language :: Python :: 3.3', | ||
'Programming Language :: Python :: 3.4', | ||
], | ||
author='Staale Undheim', | ||
author_email='[email protected]', | ||
url='http:/staale/python-xlsx', | ||
tests_require = ['six'], | ||
packages=[ | ||
"xlsx" | ||
], | ||
|
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,18 @@ | ||
[tox] | ||
envlist = py26, py27, py32, py33, py34 | ||
|
||
|
||
[testenv] | ||
usedevelop = True | ||
deps= | ||
six | ||
pytest | ||
|
||
commands = | ||
py.test xlsx/tests | ||
|
||
|
||
# py34 env not available in tox <= 1.7, so create it ourselves. | ||
[testenv:py34] | ||
basepython = python3.4 | ||
deps = {[testenv]deps} |
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 |
---|---|---|
@@ -1,11 +1,15 @@ | ||
# -*- coding: utf-8 -*- | ||
""" Small footprint xlsx reader """ | ||
|
||
from __future__ import unicode_literals | ||
|
||
__author__="Ståle Undheim <[email protected]>" | ||
|
||
import re | ||
import zipfile | ||
from xldate import xldate_as_tuple | ||
from formatting import is_date_format_string | ||
from xlsx.xldate import xldate_as_tuple | ||
from xlsx.formatting import is_date_format_string | ||
from xlsx.timemachine import UnicodeMixin | ||
|
||
try: | ||
from xml.etree import cElementTree as ET | ||
|
@@ -26,6 +30,7 @@ def __init__(self, filename): | |
""" | ||
|
||
self.ziphandle = None | ||
self.ziphandle = zipfile.ZipFile(filename, 'r') | ||
|
||
def __getitem__(self, key): | ||
|
@@ -41,7 +46,8 @@ def __getitem__(self, key): | |
def __del__(self): | ||
"""Close the zip file when finished""" | ||
|
||
self.ziphandle.close() | ||
if self.ziphandle: | ||
self.ziphandle.close() | ||
|
||
class Workbook(object): | ||
"""Main class that contains sheets organized by name or by id. | ||
|
@@ -127,17 +133,16 @@ def __init__(self, workbook, id, name): | |
self.loaded = False | ||
self.addrPattern = re.compile("([a-zA-Z]*)(\d*)") | ||
self.__cells = {} | ||
self.__cols = {} | ||
self.__rows = {} | ||
self.__cols = None | ||
self.__rows = None | ||
|
||
def __load(self): | ||
def rowsIter(self): | ||
sheetDoc = self.workbook.domzip["xl/worksheets/sheet%d.xml" % self.id] | ||
sheetData = sheetDoc.find("{http://schemas.openxmlformats.org/spreadsheetml/2006/main}sheetData") | ||
# @type sheetData Element | ||
rows = {} | ||
columns = {} | ||
for rowNode in sheetData: | ||
rowNum = int(rowNode.get("r")) | ||
rowCells = [] | ||
for columnNode in rowNode: | ||
colType = columnNode.get("t") | ||
cellId = columnNode.get("r") | ||
|
@@ -167,14 +172,22 @@ def __load(self): | |
formula = columnNode.find("{http://schemas.openxmlformats.org/spreadsheetml/2006/main}f").text | ||
except Exception: | ||
raise #pass | ||
if not rowNum in rows: | ||
rows[rowNum] = [] | ||
cell = Cell(rowNum, colNum, data, formula=formula) | ||
rowCells.append(cell) | ||
yield rowNum, rowCells | ||
|
||
def __load(self): | ||
rows = {} | ||
columns = {} | ||
for rowNum, row in self.rowsIter(): | ||
rows[rowNum] = row | ||
|
||
for cell in row: | ||
colNum = cell.column | ||
if not colNum in columns: | ||
columns[colNum] = [] | ||
cell = Cell(rowNum, colNum, data, formula=formula) | ||
rows[rowNum].append(cell) | ||
self.__cells[cell.id] = cell | ||
columns[colNum].append(cell) | ||
self.__cells[cellId] = cell | ||
self.__rows = rows | ||
self.__cols = columns | ||
self.loaded=True | ||
|
@@ -207,10 +220,16 @@ def __iter__(self): | |
self.__load() | ||
return self.__cells.__iter__() | ||
|
||
<<<<<<< HEAD | ||
def __repr__(self): | ||
return "%r[%r]"%(self.workbook, self.name) | ||
|
||
class Cell(object): | ||
======= | ||
|
||
class Cell(UnicodeMixin): | ||
|
||
>>>>>>> 5fa8fa8761d3bfcb3ce1b1b730913f6d4d0ab0c9 | ||
def __init__(self, row, column, value, formula=None): | ||
self.row = int(row) | ||
self.column = column | ||
|
@@ -248,5 +267,5 @@ def __ge__(self, other): | |
return self.__cmp__(other) != -1 | ||
|
||
def __unicode__(self): | ||
return u"<Cell [%s] : \"%s\" (%s)>" % (self.id, self.value, | ||
self.formula, ) | ||
return "<Cell [%s] : \"%s\" (%s)>" % (self.id, self.value, | ||
self.formula, ) |
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
Oops, something went wrong.