TExcelParser.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. # -*- coding:utf-8 -*-
  2. import os, sys, time
  3. import xlrd
  4. import xlwt
  5. import json
  6. # 字典不排序;
  7. from collections import OrderedDict
  8. from xlwt import XFStyle, Pattern
  9. from UIT_treeConstant import TreeConst
  10. from UIT_log import info,debug,error
  11. INFO =True
  12. DEBUG =True
  13. ERROR = True
  14. class CExcelParser():
  15. cls = "CExcelParser"
  16. def __init__(self, xls_path=None,UITree=None):
  17. self.UITree = UITree
  18. if type(xls_path) == str:
  19. xls_path = xls_path.decode('utf-8')
  20. self.xls_path = xls_path
  21. def read_excel(self, path=None):
  22. if path is not None:
  23. if type(path) == str:
  24. path = path.decode('utf-8')
  25. self.xls_path = path
  26. # 打开文件;
  27. wb = xlrd.open_workbook(filename=self.xls_path)
  28. if wb is None:
  29. return
  30. # 获取所有sheet;
  31. sheet = None
  32. for sh_name in wb.sheet_names():
  33. sheet = wb.sheet_by_name(sh_name)
  34. self.parse_excel(sheet, False)
  35. # print "self.valueParams.values:", self.valueParams.values, type(self.valueParams.values)
  36. # print "self.pathParams.paths:", self.pathParams.paths, type(self.pathParams.paths)
  37. # try:
  38. # print json.dumps(self.pathParams.paths)
  39. # print '\r\n'
  40. # print json.dumps(self.valueParams.values)
  41. # print '\r\n'
  42. # # print self.pathParams.paths,'\r\n', self.valueParams.values
  43. # except Exception, e:
  44. # print e
  45. # endfun
  46. def parse_excel(self, sheet, bpath=True):
  47. pKeys = sheet.row_values(0)
  48. info(self.cls,"parse_excel","params sheet %s keys: %s"%(sheet.name,str(pKeys)),INFO)
  49. parentName = None
  50. if u"Value" == sheet.name:
  51. for i in range(1, sheet.nrows):
  52. # 获取每行内容;
  53. oneRow = tuple(sheet.row_values(i))
  54. rowDict = {}
  55. for index in range(oneRow.__len__()):
  56. item = oneRow[index]
  57. rowDict[pKeys[index]] = item
  58. if rowDict[TreeConst.Value_Name] is None or rowDict[TreeConst.Value_Name].__len__() < 1:
  59. if parentName is None:
  60. error(self.cls, "parse_excel", "Level %s error row %s:" % (sheet.name, str(oneRow)), ERROR)
  61. break
  62. else:
  63. rowDict[TreeConst.Value_Name] = parentName
  64. self.UITree.valueParams.addOption(rowDict)
  65. elif rowDict[TreeConst.Value_Name].__len__() > 0:
  66. parentName = rowDict[TreeConst.Value_Name]
  67. self.UITree.valueParams.addParent(rowDict)
  68. elif "Dialog" == sheet.name:
  69. self.parsePath(sheet, pKeys, self.UITree.dialogParams)
  70. else: # 路径;
  71. self.parsePath(sheet,pKeys, self.UITree.pathParams)
  72. def parsePath(self, sheet,pKeys, treeParams):
  73. parentName = None
  74. for i in range(1, sheet.nrows):
  75. # 获取每行内容;
  76. oneRow = tuple(sheet.row_values(i))
  77. rowDict = {}
  78. for index in range(oneRow.__len__()):
  79. item = oneRow[index]
  80. rowDict[pKeys[index]] = item
  81. if rowDict[TreeConst.Parent_Name] is None or rowDict[TreeConst.Parent_Name].__len__() < 1:
  82. if parentName is None:
  83. error(self.cls, "parse_excel", "Level %s error row %s:" % (sheet.name, str(oneRow)), ERROR)
  84. break
  85. else:
  86. rowDict[TreeConst.Parent_Name] = parentName
  87. treeParams.addOption(sheet.name, rowDict)
  88. elif rowDict[TreeConst.FLevel_Params[0]].__len__() > 0:
  89. parentName = rowDict[TreeConst.Parent_Name]
  90. treeParams.addParent(sheet.name, rowDict)
  91. # endfun
  92. def saveExcelData(self, checkOptionPathExcelData, repeatOptionList, all_UI_TestLanguagePath):
  93. # 设置字体
  94. font = xlwt.Font()
  95. font.bold = True
  96. # 设置边框
  97. borders = xlwt.Borders()
  98. borders.left = xlwt.Borders.THIN
  99. borders.right = xlwt.Borders.THIN
  100. borders.top = xlwt.Borders.THIN
  101. borders.bottom = xlwt.Borders.THIN
  102. # 设置居中
  103. alignment = xlwt.Alignment()
  104. # alignment.horz = xlwt.Alignment.HORZ_CENTER # 水平方向
  105. alignment.horz = xlwt.Alignment.HORZ_LEFT # 水平方向
  106. alignment.vert = xlwt.Alignment.VERT_TOP # 垂直方向
  107. alignment.wrap = 1
  108. # 设置背景颜色
  109. pattern = xlwt.Pattern()
  110. pattern.pattern = xlwt.Pattern.SOLID_PATTERN
  111. pattern.pattern_fore_colour = 3 # 背景颜色
  112. # 定义不同的excel style
  113. style1 = xlwt.XFStyle()
  114. style1.font = font
  115. style1.borders = borders
  116. style1.alignment = alignment
  117. style2 = xlwt.XFStyle()
  118. style2.borders = borders
  119. style2.alignment = alignment
  120. style = XFStyle()
  121. style.borders = borders
  122. style.alignment = alignment
  123. pattern = Pattern()
  124. pattern.pattern = Pattern.SOLID_PATTERN
  125. pattern.pattern_fore_colour = xlwt.Style.colour_map['red'] # 设置单元格背景色为黄色
  126. style.pattern = pattern
  127. # style_align = xlwt.easyxf('align: wrap on')
  128. if os.path.exists(all_UI_TestLanguagePath):
  129. try:
  130. os.remove(all_UI_TestLanguagePath)
  131. except Exception, e:
  132. print e
  133. return False
  134. # 创建工作簿;
  135. book = xlwt.Workbook(encoding='utf-8')
  136. # 创建sheet;
  137. sheet = book.add_sheet(u'到达Option路径检测', cell_overwrite_ok=True)
  138. # 设置表格自适应宽度的数组
  139. col_width = [2, 25, 15, 120]
  140. # 创建标题行;
  141. row0 = [u'序号', u'Option名称', u'检测到达路径结果', u'到达Option路径']
  142. for i in range(len(row0)):
  143. sheet.write(0, i, row0[i], style=style1)
  144. index = 1
  145. for checkdata in checkOptionPathExcelData:
  146. sheet.write(index, 0, index, style=style2)
  147. for j in range(len(checkdata)):
  148. # 如果结果不为Pass则在表格中标红
  149. if j == 1 and checkdata[j] != 'Pass':
  150. sheet.write(index, j + 1, checkdata[j], style=style)
  151. else:
  152. # if j == 1:
  153. # sheet.write(index, j + 1, checkdata[j], style=style_align)
  154. # else:
  155. sheet.write(index, j + 1, checkdata[j], style=style2)
  156. # if col_width[j] < self.len_byte(checkdata[j]):
  157. # col_width[j] = self.len_byte(checkdata[j])
  158. index += 1
  159. # 设置栏位宽度,栏位宽度小于10时候采用默认宽度
  160. for i in range(len(col_width)):
  161. if col_width[i] > 10:
  162. sheet.col(i).width = 256 * (col_width[i] + 1)
  163. sheet2 = book.add_sheet(u'Option查重', cell_overwrite_ok=True)
  164. # 设置表格自适应宽度的数组
  165. col_width2 = [2, 30, 30]
  166. # 创建标题行;
  167. row02 = [u'序号', u'重复Option名称', u'重复Option在源表格的sheet名称']
  168. for i in range(len(row02)):
  169. sheet2.write(0, i, row02[i], style=style1)
  170. index2 = 1
  171. for repeatOption in repeatOptionList:
  172. sheet2.write(index2, 0, index2, style=style2)
  173. for j in range(len(repeatOption)):
  174. sheet2.write(index2, j + 1, repeatOption[j], style=style2)
  175. index2 += 1
  176. # 设置栏位宽度,栏位宽度小于10时候采用默认宽度
  177. for i in range(len(col_width2)):
  178. if col_width2[i] > 10:
  179. sheet2.col(i).width = 256 * (col_width2[i] + 1)
  180. # 保存xls;
  181. try:
  182. book.save(all_UI_TestLanguagePath)
  183. return True
  184. except Exception, e:
  185. print e
  186. return False
  187. # 新增表格查询接口
  188. # # 获取表格Value层级中,value_name下包含的value列表
  189. # def getSubValueList(self, value_name):
  190. # valueList = []
  191. # if self.valueParams.values.has_key(value_name):
  192. # # print parser.valueParams.values["source"]
  193. # # print parser.valueParams.values["source"]["value"]
  194. # valueDictList = self.valueParams.values[value_name]["value"]
  195. # for valueDict in valueDictList:
  196. # valueList.append(valueDict["value"])
  197. # # print "valueList:", valueList, type(valueList)
  198. # return valueList
  199. if __name__ == "__main__":
  200. # path = r'E:\svn\SAT\项目资料\第四期-推广使用和画面检测\06-设计文档\01-蓝图设计\UiTree3.0方案\MenuTree_MS6586_EU.xls'
  201. # path = r'E:\SAT\resource\MenuTree\MS3663\DVB-T2 panasonic\MenuTree.xls'
  202. # path = r'E:/SAT/resource\MenuTree\MS6586\ATSC\MenuTree.xls'
  203. path = r'E:\SAT\resource\MenuTree\MS6586\ATSC\MenuTree_DVBT.xls'
  204. # path = r'E:\MenuTree.xls'
  205. # path = r'E:\SAT\resource\MenuTree\6586\MS6586_ATSC\MenuTree.xls'
  206. # path = getMenuTree3SelectedProjectCfgPath() + '\\MenuTree.xls'
  207. parser = CExcelParser(path)
  208. parser.read_excel()
  209. dataDict = parser.get_parent_ocr_dict("network")
  210. print "dataDict:", dataDict, type(dataDict)
  211. # parent = "source"
  212. # option = "atv"
  213. # parser.getOptionTextList(parent, option)
  214. # value_name = "source"
  215. # value = "atv"
  216. # parser.getValueTextList(value_name, value)
  217. # SubValueList1 = parser.getSubValueList("source")
  218. # SubValueList2 = parser.getSubValueList("backlight")
  219. # parent = "picture"
  220. # parent = "manual_tuning"
  221. # parser.getSubOptionList(parent)
  222. # optionList = []
  223. # paths = parser.pathParams.paths
  224. #
  225. # for level in paths:
  226. # if paths[level].has_key(parent):
  227. # optionDictList = paths[level][parent]["value"]
  228. # for optionDict in optionDictList:
  229. # optionList.append(optionDict["option"])
  230. # print "optionList:", optionList, type(optionList)
  231. # print parser.get_value("source", "")
  232. # checkOptionPathExcelData = parser.getCheckOptionPathExcelData()
  233. # print "checkOptionPathExcelData:", checkOptionPathExcelData
  234. # repeatOptionList = parser.getRepeatOptionList()
  235. # isSaveExcelTrue = parser.saveExcelData(checkOptionPathExcelData, repeatOptionList,
  236. # r'E:\MenuTree_Check_Option_Result.xls')
  237. # path = r'E:\SAT\resource\MenuTree\6586\MS6586_ATSC\MenuTree.xls'
  238. # path = getMenuTree3SelectedProjectCfgPath() + '\\MenuTree.xls'
  239. # print "isSaveExcelTrue:", isSaveExcelTrue
  240. # valueSheetOptionList = parser.getValueSheetOptionList()
  241. # repeatValueName_ValueSheet = parser.getRepeatValueName_ValueSheet()
  242. # repeatOption_otherSheet = parser.getRepeatOption_otherSheet()
  243. # optionPath, optionPathResult = parser.checkOptionPath("picture_preset11")
  244. # print "optionPath:", optionPath, type(optionPath)
  245. # print "optionPathResult:", optionPathResult, type(optionPathResult)
  246. # optionPath, optionPathResult = parser.checkOptionPath("instant_power_on")
  247. # print "optionPath:", optionPath, type(optionPath)
  248. # print "optionPathResult:", optionPathResult, type(optionPathResult)
  249. # path_params = parser.get_option_paths("picture_preset")
  250. # print "path_params:", path_params, type(path_params)
  251. # print "path_params[0][0]:",path_params[0][0]
  252. # print parser.get_option_paths('picture')
  253. # print parser.get_option('picture')
  254. # print parser.get_value('backlight',10)
  255. # print parser.get_parent_ocr_dict('source_hdmi1')
  256. # print parser.get_parent_ocr_list('channel')
  257. # print parser.get_parent_ocr_dict('channel')
  258. # print parser.get_parent_ocr_dict('other_setting')
  259. # print parser.get_parent_ocr_list('5khz')
  260. # print parser.get_parent_ocr_dict('picture_preset', True)
  261. # print parser.get_pair_values('picture_preset', False)
  262. # print parser.get_pair_values('source')
  263. # print parser.get_ocr_list('Second','picture','picture_preset')
  264. # vp, pp = parser.get_menu_paths("auto_volume_control", "off")
  265. # print vp
  266. # vp, pp = parser.get_menu_paths("picture_preset", "dynamic")
  267. # print vp
  268. # pp = parser.get_option_paths('picture_preset')
  269. # print pp.__len__(), pp[g_level[0]]
  270. # print "\r\nvp:", vp, type(vp)
  271. # print "\r\npp:", pp, type(pp)
  272. # # print json.dumps(pp)
  273. # revpp = reversed(pp)
  274. # for item in revpp:
  275. # print item, pp[item]
  276. # print 'value:\r\n'
  277. # print vp
  278. # # print "revpp:", revpp, type(revpp)