11# -*- coding: utf-8 -*-
22""" Small footprint xlsx reader """
3+
4+ from __future__ import unicode_literals
5+
36__author__ = "Ståle Undheim <staale@staale.org>"
47
58import re
69import zipfile
7- from xldate import xldate_as_tuple
8- from formatting import is_date_format_string
10+ from xlsx .xldate import xldate_as_tuple
11+ from xlsx .formatting import is_date_format_string
12+ from xlsx .timemachine import UnicodeMixin
913
1014try :
1115 from xml .etree import cElementTree as ET
@@ -26,6 +30,7 @@ def __init__(self, filename):
2630
2731 """
2832
33+ self .ziphandle = None
2934 self .ziphandle = zipfile .ZipFile (filename , 'r' )
3035
3136 def __getitem__ (self , key ):
@@ -41,7 +46,8 @@ def __getitem__(self, key):
4146 def __del__ (self ):
4247 """Close the zip file when finished"""
4348
44- self .ziphandle .close ()
49+ if self .ziphandle :
50+ self .ziphandle .close ()
4551
4652class Workbook (object ):
4753 """Main class that contains sheets organized by name or by id.
@@ -127,17 +133,16 @@ def __init__(self, workbook, id, name):
127133 self .loaded = False
128134 self .addrPattern = re .compile ("([a-zA-Z]*)(\d*)" )
129135 self .__cells = {}
130- self .__cols = {}
131- self .__rows = {}
136+ self .__cols = None
137+ self .__rows = None
132138
133- def __load (self ):
139+ def rowsIter (self ):
134140 sheetDoc = self .workbook .domzip ["xl/worksheets/sheet%d.xml" % self .id ]
135141 sheetData = sheetDoc .find ("{http://schemas.openxmlformats.org/spreadsheetml/2006/main}sheetData" )
136142 # @type sheetData Element
137- rows = {}
138- columns = {}
139143 for rowNode in sheetData :
140144 rowNum = int (rowNode .get ("r" ))
145+ rowCells = []
141146 for columnNode in rowNode :
142147 colType = columnNode .get ("t" )
143148 cellId = columnNode .get ("r" )
@@ -167,14 +172,22 @@ def __load(self):
167172 formula = columnNode .find ("{http://schemas.openxmlformats.org/spreadsheetml/2006/main}f" ).text
168173 except Exception :
169174 raise #pass
170- if not rowNum in rows :
171- rows [rowNum ] = []
175+ cell = Cell (rowNum , colNum , data , formula = formula )
176+ rowCells .append (cell )
177+ yield rowNum , rowCells
178+
179+ def __load (self ):
180+ rows = {}
181+ columns = {}
182+ for rowNum , row in self .rowsIter ():
183+ rows [rowNum ] = row
184+
185+ for cell in row :
186+ colNum = cell .column
172187 if not colNum in columns :
173188 columns [colNum ] = []
174- cell = Cell (rowNum , colNum , data , formula = formula )
175- rows [rowNum ].append (cell )
189+ self .__cells [cell .id ] = cell
176190 columns [colNum ].append (cell )
177- self .__cells [cellId ] = cell
178191 self .__rows = rows
179192 self .__cols = columns
180193 self .loaded = True
@@ -207,10 +220,16 @@ def __iter__(self):
207220 self .__load ()
208221 return self .__cells .__iter__ ()
209222
223+ < << << << HEAD
210224 def __repr__ (self ):
211225 return "%r[%r]" % (self .workbook , self .name )
212226
213227class Cell (object ):
228+ == == == =
229+
230+ class Cell (UnicodeMixin ):
231+
232+ > >> >> >> 5 fa8fa8761d3bfcb3ce1b1b730913f6d4d0ab0c9
214233 def __init__ (self , row , column , value , formula = None ):
215234 self .row = int (row )
216235 self .column = column
@@ -248,5 +267,5 @@ def __ge__(self, other):
248267 return self .__cmp__ (other ) != - 1
249268
250269 def __unicode__ (self ):
251- return u "<Cell [%s] : \" %s\" (%s)>" % (self .id , self .value ,
252- self .formula , )
270+ return "<Cell [%s] : \" %s\" (%s)>" % (self .id , self .value ,
271+ self .formula , )
0 commit comments