def __checkData(self): """ 列数据是否符合命名规范, 生成所需字典 """ self.sheetIndex2Data() self.dctDatas = self.g_dctDatas self.hasExportedSheet = [] for dataName, indexList in self.sheet2Data.items(): self.curIndexMax = len(indexList) self.curProIndex = [] for index in indexList: sheet = self.xbook.getSheetByIndex(index) self.curProIndex.append(index) cols = self.xbook.getRowCount(index) rows = self.xbook.getColCount(index) if dataName not in self.dctDatas: self.dctDatas[dataName] = {} self.dctData = self.dctDatas[dataName] for row in range(EXPORT_DATA_ROW, rows + 1): rowval = self.xbook.getRowValues(sheet, row - 1) childDict = {} for col in range(1, cols + 1): val = rowval[col - 1] if val != None: val = (str(rowval[col - 1]),) else: val = ("",) #val = (self.xbook.getText(sheet, row, col),) if self.headerDict[index][col-1] is None: continue name, sign, funcName = self.headerDict[index][col-1] if EXPORT_SIGN_DOLLAR in sign and len(val[0]) > 0: self.needReplace({'v':val[0], "pos":(row, col)}) v = self.mapDict[xlsxtool.GTOUC(xlsxtool.val2Str(val[0]))] #mapDict:key是unicode.key都要转成unicode else: v = val[0] if EXPORT_SIGN_DOT in sign and (v is None or len(v) == 0) : self.xlsxClear(EXPORT_ERROR_NOTNULL, (col, row)) try: sv = v#xlsxtool.toGBK(v) except: sv = v func = getFunc(funcName) try: v = func(self.mapDict, self.dctData, childDict, sv) except Exception as errstr: self.xlsxClear(EXPORT_ERROR_FUNC, (errstr, funcName, sv, 'row:'+str(row), 'col:'+str(col) )) for ss in sign.replace('$',''): if EXPORT_SIGN[ss] != None : EXPORT_SIGN[ss](self,{"v":v,"pos":(row, col)}) #if isinstance(v, (isinstance, unicode)): # try: # v = v.decode("gb2312").encode("utf-8") # except: # pass childDict[name] = v print( "当前:%i/%i" % (row, rows) ) self.dctData[self.tempKeys[-1]] = copy.deepcopy(childDict) self.checkGlobalDef() #如果有最终检查处理函数,则调用检查 overFunc = self.mapDict.get('overFunc') if overFunc is not None and hasFunc(overFunc): func = getFunc(overFunc) self.dctData = func(self.mapDict, self.dctDatas, self.dctData, dataName) self.dctDatas[dataName] = self.dctData self.g_dctDatas.update(self.dctDatas) self.__onCheckSheet() self.__onCheckData() self.writeFoot()
def __checkData(self): """ 列数据是否符合命名规范, 生成所需字典 """ self.sheetName2Data() self.dctDatas = g_dctDatas self.hasExportedSheet = [] for dataName in self.sheet2Data: print('开始处理表:%s' % dataName) sheetName = self.sheet2Data[dataName] sheet = self.xbook.getSheetBySheetName(sheetName) rows = self.xbook.getRowCount(sheetName) cols = self.xbook.getColCount(sheetName) if dataName not in self.dctDatas: self.dctDatas[dataName] = {} self.dctData = self.dctDatas[dataName] # for row in range(3, rows + 1): for row in tqdm.tqdm(range(3, rows + 1), ncols=50): keyName: str = None rowval = self.xbook.getRowValues(sheet, row - 1) childDict = {} for col in range(1, cols + 1): val = rowval[col - 1] if val is not None: val = (str(rowval[col - 1]), ) else: val = ("", ) if self.headerDict[sheetName][col - 1] is None: continue name, sign, funcName = self.headerDict[sheetName][col - 1] if '$' in sign and len(val[0]) > 0: self.needReplace({'v': val[0], "pos": (row, col)}) if ',' in val[0]: nv = val[0].strip() vs = nv.split(',') v = '' for item in vs: v += (self.mapDict[xlsxtool.GTOUC( xlsxtool.val2Str(item))] + ',') v = v[:-1] # 去掉最后的',' else: # mapDict:key是unicode.key都要转成unicode v = self.mapDict[xlsxtool.GTOUC( xlsxtool.val2Str(val[0]))] else: v = val[0] if config.EXPORT_SIGN_DOT in sign and v is None: self.xlsxClear(config.EXPORT_ERROR_NOTNULL, (col, row)) sv = v func = getFunc(funcName) try: v = func(self.mapDict, self.dctData, childDict, sv) except Exception as errstr: self.xlsxClear(config.EXPORT_ERROR_FUNC, (errstr, funcName, sv, row, col)) for ss in sign.replace('$', ''): if len(sv) == 0 and ss == '!': continue if ss == '!': keyName = name config.EXPORT_SIGN[ss](self, { 'tableName': dataName, "v": v, "pos": (row, col) }) childDict[name] = v if keyName is not None: self.dctData[childDict[keyName]] = copy.deepcopy(childDict) overFunc = self.mapDict.get('overFunc') if overFunc is not None: func = getFunc(overFunc) self.dctData = func(self.mapDict, self.dctDatas, self.dctData, dataName) self.dctDatas[dataName] = self.dctData self.tempKeys.clear() g_dctDatas.update(self.dctDatas) self.writeBody()