# -*- coding: UTF-8 -*- import xml.etree.ElementTree as ET import re import os XMLSS = '{urn:schemas-microsoft-com:office:spreadsheet}' FLOAT_re = re.compile(r'^-?\d+(\.\d+)?$') empty_re = re.compile(r'^\s*$') Type_commont = XMLSS +'Comment' def GetValue (s,cell): if cell.text != None: s+=cell.text if len(cell._children) == 0: return s l = [GetValue("",cs) for cs in cell._children if cs.tag != Type_commont ] for a in l: s+=a # return GetValue(s,cell[0]) return s def FloatToString (aFloat): strTemp = str(aFloat) if aFloat == None or empty_re.match(aFloat) != None: return '0'; if FLOAT_re.match(aFloat) == None: print aFloat + u" - 数字类型格式错误" return False strList = strTemp.split(".") if len(strList) == 1 : return strTemp else: if strList[1] == "0" : return strList[0] else: return strTemp def NumberToString (aFloat): ret = FloatToString(aFloat) if ret == False: return False return str(int(float(ret))) def haveNext(table, col, row, max_cols): for c in range(col, max_cols): objType = str(table.cell_value(row, c)); if(objType.find("@") == -1): return 1 return 0 def int2index(idx): pos = chr(idx % 26 + 65) if idx > 25: a = int(idx / 26-1) pos = int2index(a)+pos return pos def StringToString (aStr): if type(aStr) != float: return str(aStr) strTemp = str(aStr) strList = strTemp.split(".") if len(strList) == 1 : return strTemp else: if strList[1] == "0" : return strList[0] else: return strTemp class Sheet(object): def __init__(self,sheet): self.sheet=sheet self.name=sheet.get(XMLSS+'Name') self.rows = [r for r in sheet.find(XMLSS+'Table').findall(XMLSS+'Row') if len(r._children) > 0 and any([c.find(XMLSS+'Data') != None for c in r._children]) ] self.nrows=len(self.rows) if self.nrows > 0: self.ncols=len(self.rows[0]._children) def cell_value(self,r,c): if c >= len(self.rows[r]): return "" cell = self.rows[r][c] if cell == None: return "" idx = cell.get(XMLSS+'Index') if idx != None: idx=int(idx) if idx-1 != c: self.rows[r].insert(c,None) return "" if cell.find(XMLSS+'Data') == None: return "" return GetValue("",self.rows[r][c]) def exception(): os.system('pause') exit(1) def openxml(file): sheets = ET.parse(file).getroot().findall(XMLSS+'Worksheet') return [ Sheet(s) for s in sheets ]