123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- import os, sys, time
- import xlrd
- import xlwt
- import json
- from collections import OrderedDict
- from xlwt import XFStyle, Pattern
- from UIT_treeConstant import TreeConst
- from UIT_log import info,debug,error
- INFO =True
- DEBUG =True
- ERROR = True
- class CExcelParser():
- cls = "CExcelParser"
- def __init__(self, xls_path=None,UITree=None):
- self.UITree = UITree
- if type(xls_path) == str:
- xls_path = xls_path.decode('utf-8')
- self.xls_path = xls_path
- def read_excel(self, path=None):
- if path is not None:
-
- if type(path) == str:
- path = path.decode('utf-8')
- self.xls_path = path
-
- wb = xlrd.open_workbook(filename=self.xls_path)
- if wb is None:
- return
-
- sheet = None
- for sh_name in wb.sheet_names():
- sheet = wb.sheet_by_name(sh_name)
- self.parse_excel(sheet, False)
-
-
-
-
-
-
-
-
-
-
-
- def parse_excel(self, sheet, bpath=True):
-
- pKeys = sheet.row_values(0)
- info(self.cls,"parse_excel","params sheet %s keys: %s"%(sheet.name,str(pKeys)),INFO)
-
- parentName = None
- if u"Value" == sheet.name:
-
- for i in range(1, sheet.nrows):
- rowDict = {}
-
- oneRow = tuple(sheet.row_values(i))
-
- for index in range(oneRow.__len__()):
- item = oneRow[index]
- rowDict[pKeys[index]] = item
- if rowDict[TreeConst.Value_Name] is None or rowDict[TreeConst.Value_Name].__len__() < 1:
- if parentName is None:
- error(self.cls, "parse_excel", "Level %s error row %s:" % (sheet.name, str(oneRow)), ERROR)
- break
- else:
- rowDict[TreeConst.Value_Name] = parentName
- self.UITree.valueParams.addOption(rowDict)
- elif rowDict[TreeConst.Value_Name].__len__() > 0:
- parentName = rowDict[TreeConst.Value_Name]
- self.UITree.valueParams.addParent(rowDict)
- elif "Dialog" == sheet.name:
- self.parsePath(sheet, pKeys, self.UITree.dialogParams)
- else:
- self.parsePath(sheet,pKeys, self.UITree.pathParams)
- def parsePath(self, sheet,pKeys, treeParams):
- parentName = None
- for i in range(1, sheet.nrows):
-
- oneRow = tuple(sheet.row_values(i))
- rowDict = {}
- for index in range(oneRow.__len__()):
- item = oneRow[index]
- rowDict[pKeys[index]] = item
- if rowDict[TreeConst.Parent_Name] is None or rowDict[TreeConst.Parent_Name].__len__() < 1:
- if parentName is None:
- error(self.cls, "parse_excel", "Level %s error row %s:" % (sheet.name, str(oneRow)), ERROR)
- break
- else:
- rowDict[TreeConst.Parent_Name] = parentName
- treeParams.addOption(sheet.name, rowDict)
- elif rowDict[TreeConst.FLevel_Params[0]].__len__() > 0:
- parentName = rowDict[TreeConst.Parent_Name]
- treeParams.addParent(sheet.name, rowDict)
-
- def saveExcelData(self, checkOptionPathExcelData, repeatOptionList, all_UI_TestLanguagePath):
-
- font = xlwt.Font()
- font.bold = True
-
- borders = xlwt.Borders()
- borders.left = xlwt.Borders.THIN
- borders.right = xlwt.Borders.THIN
- borders.top = xlwt.Borders.THIN
- borders.bottom = xlwt.Borders.THIN
-
- alignment = xlwt.Alignment()
-
- alignment.horz = xlwt.Alignment.HORZ_LEFT
- alignment.vert = xlwt.Alignment.VERT_TOP
- alignment.wrap = 1
-
- pattern = xlwt.Pattern()
- pattern.pattern = xlwt.Pattern.SOLID_PATTERN
- pattern.pattern_fore_colour = 3
-
- style1 = xlwt.XFStyle()
- style1.font = font
- style1.borders = borders
- style1.alignment = alignment
- style2 = xlwt.XFStyle()
- style2.borders = borders
- style2.alignment = alignment
- style = XFStyle()
- style.borders = borders
- style.alignment = alignment
- pattern = Pattern()
- pattern.pattern = Pattern.SOLID_PATTERN
- pattern.pattern_fore_colour = xlwt.Style.colour_map['red']
- style.pattern = pattern
-
- if os.path.exists(all_UI_TestLanguagePath):
- try:
- os.remove(all_UI_TestLanguagePath)
- except Exception, e:
- print e
- return False
-
- book = xlwt.Workbook(encoding='utf-8')
-
- sheet = book.add_sheet(u'到达Option路径检测', cell_overwrite_ok=True)
-
- col_width = [2, 25, 15, 120]
-
- row0 = [u'序号', u'Option名称', u'检测到达路径结果', u'到达Option路径']
- for i in range(len(row0)):
- sheet.write(0, i, row0[i], style=style1)
- index = 1
- for checkdata in checkOptionPathExcelData:
- sheet.write(index, 0, index, style=style2)
- for j in range(len(checkdata)):
-
- if j == 1 and checkdata[j] != 'Pass':
- sheet.write(index, j + 1, checkdata[j], style=style)
- else:
-
-
-
- sheet.write(index, j + 1, checkdata[j], style=style2)
-
-
- index += 1
-
- for i in range(len(col_width)):
- if col_width[i] > 10:
- sheet.col(i).width = 256 * (col_width[i] + 1)
- sheet2 = book.add_sheet(u'Option查重', cell_overwrite_ok=True)
-
- col_width2 = [2, 30, 30]
-
- row02 = [u'序号', u'重复Option名称', u'重复Option在源表格的sheet名称']
- for i in range(len(row02)):
- sheet2.write(0, i, row02[i], style=style1)
- index2 = 1
- for repeatOption in repeatOptionList:
- sheet2.write(index2, 0, index2, style=style2)
- for j in range(len(repeatOption)):
- sheet2.write(index2, j + 1, repeatOption[j], style=style2)
- index2 += 1
-
- for i in range(len(col_width2)):
- if col_width2[i] > 10:
- sheet2.col(i).width = 256 * (col_width2[i] + 1)
-
- try:
- book.save(all_UI_TestLanguagePath)
- return True
- except Exception, e:
- print e
- return False
-
-
-
-
-
-
-
-
-
-
-
-
- if __name__ == "__main__":
-
-
-
- path = r'E:\SAT\resource\MenuTree\MS6586\ATSC\MenuTree_DVBT.xls'
-
-
-
- parser = CExcelParser(path)
- parser.read_excel()
- dataDict = parser.get_parent_ocr_dict("network")
- print "dataDict:", dataDict, type(dataDict)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|