TExcelParser.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. g_level = ['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh', 'Eighth', 'Ninth', 'Tenth', 'Eleventh',
  10. 'Twelfth']
  11. class CPathParams():
  12. def __init__(self):
  13. self.paths = OrderedDict()
  14. def add_name(self, level, parent, move_key, enter_key, others):
  15. if level not in self.paths:
  16. self.paths[level] = {}
  17. # endif
  18. move_key = move_key.replace(' ', '')
  19. self.paths[level][parent] = {"move_key": move_key.split(';'), "enter_key": enter_key, "others": others,
  20. "value": []}
  21. def add_item(self, level, parent, option, ocr, move_key, enter_key, others):
  22. if level in self.paths:
  23. if parent not in self.paths[level]:
  24. self.paths[level][parent] = {}
  25. else:
  26. self.paths[level] = {}
  27. self.paths[level][parent] = {}
  28. self.paths[level][parent]["value"].append(
  29. {"option": option, "option_for_ocr": ocr, "option_move_key": move_key, "option_enter_key": enter_key,
  30. "option_others": others})
  31. class CValueParams():
  32. def __init__(self):
  33. self.values = OrderedDict()
  34. def add_name(self, name, move_key, enter_key, others):
  35. if name not in self.values:
  36. move_key = move_key.replace(' ', '')
  37. self.values[name] = {"move_key": move_key.split(';'), "enter_key": enter_key, "others": others, "value": []}
  38. def add_item(self, name, option, ocr):
  39. if name in self.values:
  40. self.values[name]["value"].append({"value": option, "value_for_ocr": ocr})
  41. class CExcelParser():
  42. def __init__(self, xls_path):
  43. if type(xls_path) == str:
  44. xls_path = xls_path.decode('utf-8')
  45. self.xls_path = xls_path
  46. self.pathParams = CPathParams()
  47. self.valueParams = CValueParams()
  48. def read_excel(self, path=None):
  49. if path is not None:
  50. if type(path) == str:
  51. path = path.decode('utf-8')
  52. self.xls_path = path
  53. # 打开文件;
  54. wb = xlrd.open_workbook(filename=self.xls_path)
  55. if wb is None:
  56. return
  57. # 获取所有sheet;
  58. sheet = None
  59. for sh_name in wb.sheet_names():
  60. sheet = wb.sheet_by_name(sh_name)
  61. self.parse_excel(sheet, False)
  62. # print "self.valueParams.values:", self.valueParams.values, type(self.valueParams.values)
  63. # print "self.pathParams.paths:", self.pathParams.paths, type(self.pathParams.paths)
  64. # try:
  65. # print json.dumps(self.pathParams.paths)
  66. # print '\r\n'
  67. # print json.dumps(self.valueParams.values)
  68. # print '\r\n'
  69. # # print self.pathParams.paths,'\r\n', self.valueParams.values
  70. # except Exception, e:
  71. # print e
  72. # endfun
  73. def parse_excel(self, sheet, bpath=True):
  74. last_name = None
  75. if u"Value" == sheet.name:
  76. for i in range(1, sheet.nrows):
  77. # 获取每行内容;
  78. rows = tuple(sheet.row_values(i))
  79. if rows[1].__len__() > 0 and rows[2].__len__() > 0 and last_name.__len__() > 0:
  80. # print sheet.name,"last_name:",last_name
  81. self.valueParams.add_item(last_name, rows[1], rows[2])
  82. elif rows[0].__len__() > 0:
  83. last_name = rows[0]
  84. # print sheet.name, "last_name:", last_name
  85. self.valueParams.add_name(last_name, rows[3], rows[4], rows[5])
  86. else: # 路径;
  87. for i in range(1, sheet.nrows):
  88. # 获取每行内容;
  89. rows = tuple(sheet.row_values(i))
  90. if rows[1].__len__() > 0 and rows[2].__len__() > 0 and last_name.__len__() > 0:
  91. move_key = rows[3].split(';')
  92. self.pathParams.add_item(sheet.name, last_name, rows[1], rows[2], move_key, rows[4], rows[5])
  93. elif rows[0].__len__() > 0:
  94. last_name = rows[0]
  95. self.pathParams.add_name(sheet.name, last_name, rows[3], rows[4], rows[5])
  96. # endfun
  97. def get_menu_paths(self, option, value):
  98. if type(option) == str:
  99. option = unicode(option)
  100. if type(value) == str:
  101. value = unicode(value)
  102. # pp必须不排序;
  103. vp, pp = {}, OrderedDict()
  104. if option in self.valueParams.values:
  105. vp = {"option": option, "value": value, "enter_key": self.valueParams.values[option]["enter_key"],
  106. "move_key": self.valueParams.values[option]["move_key"]}
  107. # 找到value对应的ocr;
  108. for item in self.valueParams.values[option]["value"]:
  109. if "range(" in item["value"]:
  110. vp["value_for_ocr"] = item["value_for_ocr"].split(';')
  111. break
  112. elif item["value"].lower() == value.lower():
  113. vp["value_for_ocr"] = item["value_for_ocr"].split(';')
  114. break
  115. # 首先,字典不排序,需要倒序;
  116. paths = reversed(self.pathParams.paths)
  117. for level in paths:
  118. found = False
  119. for parent in self.pathParams.paths[level]:
  120. # print parent,self.pathParams.paths[level][parent],'\r\n'
  121. for item in self.pathParams.paths[level][parent]["value"]:
  122. # print item
  123. if item["option"].lower() == option.lower():
  124. pp[level] = {
  125. "parent": parent,
  126. "move_key": self.pathParams.paths[level][parent]["move_key"],
  127. "enter_key": self.pathParams.paths[level][parent]["enter_key"],
  128. "others": self.pathParams.paths[level][parent]["others"],
  129. "option": item["option"],
  130. "option_for_ocr": item["option_for_ocr"].split(';'),
  131. "option_move_key": item["option_move_key"],
  132. "option_enter_key": item["option_enter_key"],
  133. "option_others": item["option_others"]
  134. }
  135. option = parent
  136. found = True
  137. break
  138. if found:
  139. break
  140. else:
  141. if parent == option:
  142. for item in self.pathParams.paths[level][parent]["value"]:
  143. # print item
  144. if item["option"].lower() == value.lower():
  145. pp[level] = {
  146. "parent": parent,
  147. "move_key": self.pathParams.paths[level][parent]["move_key"],
  148. "enter_key": self.pathParams.paths[level][parent]["enter_key"],
  149. "others": self.pathParams.paths[level][parent]["others"],
  150. "option": item["option"],
  151. "option_for_ocr": item["option_for_ocr"].split(';'),
  152. "option_move_key": item["option_move_key"],
  153. "option_enter_key": item["option_enter_key"],
  154. "option_others": item["option_others"]
  155. }
  156. option = parent
  157. found = True
  158. break
  159. # endif
  160. # endfor
  161. break
  162. # endif
  163. # 需要对path倒序使用;
  164. return vp, pp
  165. def get_option_paths(self, option):
  166. # pp必须不排序;
  167. pp = OrderedDict()
  168. # 首先,字典不排序,需要倒序;
  169. paths = reversed(self.pathParams.paths)
  170. for level in paths:
  171. found = False
  172. for parent in self.pathParams.paths[level]:
  173. # print parent,self.pathParams.paths[level][parent],'\r\n'
  174. for item in self.pathParams.paths[level][parent]["value"]:
  175. # print item
  176. if item["option"].lower() == option.lower():
  177. pp[level] = {
  178. "parent": parent,
  179. "move_key": self.pathParams.paths[level][parent]["move_key"],
  180. "enter_key": self.pathParams.paths[level][parent]["enter_key"],
  181. "others": self.pathParams.paths[level][parent]["others"],
  182. "option": item["option"],
  183. "option_for_ocr": item["option_for_ocr"].split(';'),
  184. "option_move_key": item["option_move_key"],
  185. "option_enter_key": item["option_enter_key"],
  186. "option_others": item["option_others"]
  187. }
  188. option = parent
  189. found = True
  190. break
  191. if found:
  192. break
  193. else:
  194. if parent == option:
  195. for item in self.pathParams.paths[level][parent]["value"]:
  196. # print item
  197. if item["option"].lower() == option.lower():
  198. pp[level] = {
  199. "parent": parent,
  200. "move_key": self.pathParams.paths[level][parent]["move_key"],
  201. "enter_key": self.pathParams.paths[level][parent]["enter_key"],
  202. "others": self.pathParams.paths[level][parent]["others"],
  203. "option": item["option"],
  204. "option_for_ocr": item["option_for_ocr"].split(';'),
  205. "option_move_key": item["option_move_key"],
  206. "option_enter_key": item["option_enter_key"],
  207. "option_others": item["option_others"]
  208. }
  209. option = parent
  210. found = True
  211. break
  212. # endif
  213. # endfor
  214. break
  215. # endif
  216. # endfor
  217. # endfor
  218. # 需要对path倒序使用;
  219. # dict_pp = OrderedDict()
  220. # # 逆序路径key;
  221. # revpp = reversed(pp)
  222. # for path in revpp:
  223. # dict_pp[path] = {"parent": pp[path]['parent'],"move_key": pp[path]['move_key'],"enter_key": pp[path]['enter_key'],"others":pp[path]['others'],"option": pp[path]['option'],"option_for_ocr": pp[path]['option_for_ocr']}
  224. # # 返回逆序后的结果;
  225. # return dict_pp
  226. return pp
  227. # 获取键对值
  228. def get_pair_values(self, name, option=True):
  229. if type(name) == str:
  230. name = unicode(name)
  231. pairs = []
  232. if option is True:
  233. find = False
  234. for level in self.pathParams.paths:
  235. for item in self.pathParams.paths[level]:
  236. if item.lower() == name.lower():
  237. find = True
  238. for item in self.pathParams.paths[level][name]["value"]:
  239. pairs.append({item['option']: item['option_for_ocr'].split(';')})
  240. break
  241. # endfor
  242. if find is True:
  243. break
  244. # endfor
  245. else:
  246. if name in self.valueParams.values:
  247. for item in self.valueParams.values[name]["value"]:
  248. pairs.append({item['value']: item['value_for_ocr'].split(';')})
  249. # endif
  250. return pairs
  251. # 获取指定层级的path或value的xxx_for_ocr列表;
  252. def get_ocr_list(self, level, name, option, is_option=True):
  253. if type(name) == str:
  254. name = unicode(name)
  255. if type(option) == str:
  256. option = unicode(option)
  257. list_ocr = []
  258. if is_option is True:
  259. find = False
  260. for item in self.pathParams.paths[level]:
  261. if item.lower() == name.lower():
  262. find = True
  263. for item in self.pathParams.paths[level][name]["value"]:
  264. if str(option).lower() != str(item["option"]).lower():
  265. list_ocr.append(item['option_for_ocr'].split(';'))
  266. break
  267. # endfor
  268. else:
  269. if name in self.valueParams.values:
  270. for item in self.valueParams.values[name]["value"]:
  271. list_ocr.append(item['value_for_ocr'].split(';'))
  272. # endif
  273. return list_ocr
  274. # 获取指定value_name所在菜单项的全部同级菜单项ocr列表;
  275. def get_parent_ocr_list(self, option):
  276. # 编码转换;
  277. if type(option) == str:
  278. option = unicode(option)
  279. found = False
  280. list_ocr = []
  281. # 首先,字典不排序,需要倒序;
  282. paths = reversed(self.pathParams.paths)
  283. for level in paths:
  284. for parent in self.pathParams.paths[level]:
  285. for vals in self.pathParams.paths[level][parent]["value"]:
  286. if vals['option'].lower() == option.lower():
  287. found = True
  288. break
  289. # endfor
  290. # 找到退出;
  291. if found is True:
  292. break
  293. # endfor
  294. # 找到退出;
  295. if found is True:
  296. break
  297. # endfor
  298. # 遍历value列表;
  299. if found is True:
  300. for val in self.pathParams.paths[level][parent]["value"]:
  301. list_ocr.extend(val['option_for_ocr'].split(';'))
  302. # 按长度排序;
  303. list_ocr.sort(key=lambda i: len(i), reverse=True)
  304. # 返回结果;
  305. return list_ocr
  306. # 当value_sheet = False,表示获取path表里的option层级的所有option_for_ocr:option
  307. # 当value_sheet = True,表示获取value表里的value_name层级的所有value_for_ocr:value
  308. def get_parent_ocr_dict(self, option, value_sheet=False):
  309. print "get_parent_ocr_dict.option:", option
  310. # 编码转换;
  311. if type(option) == str:
  312. option = unicode(option)
  313. found = False
  314. dict_ocr = {}
  315. list_ocr = []
  316. if value_sheet is False:
  317. # 首先,字典不排序,需要倒序;
  318. paths = reversed(self.pathParams.paths)
  319. for level in paths:
  320. for parent in self.pathParams.paths[level]:
  321. for vals in self.pathParams.paths[level][parent]["value"]:
  322. if vals['option'].lower() == option.lower():
  323. found = True
  324. break
  325. # endfor
  326. # 找到退出;
  327. if found is True:
  328. break
  329. # endfor
  330. # 找到退出;
  331. if found is True:
  332. break
  333. # 遍历value列表;
  334. if found is True:
  335. for val in self.pathParams.paths[level][parent]["value"]:
  336. list_ocr = val['option_for_ocr'].split(';')
  337. for ocr in list_ocr:
  338. dict_ocr[ocr.lower()] = val['option']
  339. else:
  340. if option in self.valueParams.values:
  341. for val in self.valueParams.values[option]["value"]:
  342. list_ocr = val['value_for_ocr'].split(';')
  343. for ocr in list_ocr:
  344. dict_ocr[ocr.lower()] = val['value']
  345. print 'unsorted=', dict_ocr
  346. # 按长度排序;
  347. list_ocr = sorted(dict_ocr.keys(), key=lambda key: len(key), reverse=True)
  348. dict_reuslt = OrderedDict()
  349. for ocr in list_ocr:
  350. dict_reuslt[ocr] = dict_ocr[ocr]
  351. # 返回结果;
  352. return dict_reuslt
  353. def get_option_ocr(self, option):
  354. # 编码转换;
  355. if type(option) == str:
  356. option = unicode(option)
  357. found = False
  358. list_ocr = []
  359. # 首先,字典不排序,需要倒序;
  360. paths = reversed(self.pathParams.paths)
  361. for level in paths:
  362. for parent in self.pathParams.paths[level]:
  363. for vals in self.pathParams.paths[level][parent]["value"]:
  364. if vals['option'].lower() == option.lower():
  365. list_ocr = vals['option_for_ocr'].split(';')
  366. found = True
  367. break
  368. # endfor
  369. # 找到退出;
  370. if found is True:
  371. break
  372. # endfor
  373. # 找到退出;
  374. if found is True:
  375. break
  376. # endfor
  377. # 返回结果;
  378. return list_ocr
  379. def get_value(self, option, value=""):
  380. # 编码转换;
  381. if type(option) == str:
  382. option = unicode(option)
  383. xlsValue = self.valueParams.values[option]["value"]
  384. valueList = []
  385. valueOcrList = []
  386. for item in xlsValue:
  387. valueList.append(item["value"])
  388. valueOcrList.append(item["value_for_ocr"])
  389. vp = {}
  390. if option in self.valueParams.values:
  391. vp = {"option": option, "value": valueList, "value_for_ocr": valueOcrList,
  392. "enter_key": self.valueParams.values[option]["enter_key"],
  393. "move_key": self.valueParams.values[option]["move_key"],
  394. "others": self.valueParams.values[option]['others']}
  395. # 找到value对应的ocr;
  396. if value != "":
  397. vp["value"] = value
  398. for item in self.valueParams.values[option]["value"]:
  399. if "range(" in item["value"]:
  400. vp["value_for_ocr"] = item["value_for_ocr"].split(';')
  401. break
  402. elif item["value"].lower() == value.lower():
  403. vp["value_for_ocr"] = item["value_for_ocr"].split(';')
  404. break
  405. # endfor
  406. # endif
  407. # endif
  408. # 返回结果;
  409. return vp
  410. # 获取option层级内容;
  411. def get_option(self, option):
  412. # 编码转换;
  413. if type(option) == str:
  414. option = unicode(option)
  415. found = False
  416. layers = 0
  417. dict_option = {}
  418. # 首先,字典不排序,需要倒序;
  419. paths = reversed(self.pathParams.paths)
  420. for level in paths:
  421. for parent in self.pathParams.paths[level]:
  422. for vals in self.pathParams.paths[level][parent]["value"]:
  423. if vals['option'].lower() == option.lower():
  424. if dict_option.__len__() == 0:
  425. found = True
  426. dict_option['level'] = level
  427. dict_option['parent'] = parent
  428. dict_option['others'] = self.pathParams.paths[level][parent]['others']
  429. dict_option['enter_key'] = self.pathParams.paths[level][parent]['enter_key']
  430. dict_option['move_key'] = self.pathParams.paths[level][parent]['move_key']
  431. dict_option['option_ocr'] = vals['option_for_ocr'].split(';')
  432. dict_option['option_move_key'] = vals['option_move_key']
  433. dict_option['option_enter_key'] = vals['option_enter_key']
  434. dict_option['option_others'] = vals['option_others']
  435. option = parent
  436. layers += 1 # 层数;
  437. break
  438. # endfor
  439. # endfor
  440. # endfor
  441. if found is True:
  442. dict_option['layers'] = layers
  443. # first层次的parent名称;
  444. dict_option['first_parent'] = option
  445. # 返回结果;
  446. return found, dict_option
  447. #
  448. def getRepeatValueName_ValueSheet(self):
  449. wb = xlrd.open_workbook(filename=self.xls_path)
  450. valueSheet = wb.sheet_by_name("Value")
  451. rows = valueSheet.nrows # 获取行数
  452. all_value_option_list = []
  453. RepeatValueName_ValueSheet_List = []
  454. for i in range(1, rows):
  455. # 获取每行内容;
  456. rows = tuple(valueSheet.row_values(i))
  457. if rows[0].__len__() > 0:
  458. if rows[0] in all_value_option_list:
  459. RepeatValueName_ValueSheet_List.append([rows[0], "Value"])
  460. else:
  461. all_value_option_list.append(rows[0])
  462. print "RepeatValueName_ValueSheet_List:", RepeatValueName_ValueSheet_List
  463. return RepeatValueName_ValueSheet_List
  464. def getRepeatOption_otherSheet(self):
  465. wb = xlrd.open_workbook(filename=self.xls_path)
  466. all_option_list = []
  467. repeatOption_otherSheet_List = []
  468. for sh_name in wb.sheet_names():
  469. sheet = wb.sheet_by_name(sh_name)
  470. if u"Value" == sheet.name:
  471. pass
  472. else: # 路径;
  473. for i in range(1, sheet.nrows):
  474. # 获取每行内容;
  475. rows = tuple(sheet.row_values(i))
  476. if rows[1].__len__() > 0:
  477. if rows[1] in all_option_list:
  478. repeatOption_otherSheet_List.append([rows[1], sheet.name])
  479. # print "rows[1]:", rows[1], "sheetName:", sheet.name
  480. else:
  481. all_option_list.append(rows[1])
  482. print "repeatOption_otherSheet_List:", repeatOption_otherSheet_List
  483. return repeatOption_otherSheet_List
  484. def getRepeatOptionList(self):
  485. RepeatOptionList = []
  486. repeatValueName_ValueSheet = self.getRepeatValueName_ValueSheet()
  487. repeatOption_otherSheet = self.getRepeatOption_otherSheet()
  488. RepeatOptionList = repeatOption_otherSheet + repeatValueName_ValueSheet
  489. print "RepeatOptionList:", RepeatOptionList
  490. return RepeatOptionList
  491. def getValueSheetOptionList(self):
  492. wb = xlrd.open_workbook(filename=self.xls_path)
  493. valueSheet = wb.sheet_by_name("Value")
  494. rows = valueSheet.nrows # 获取行数
  495. all_value_option_list = []
  496. for i in range(1, rows):
  497. # 获取每行内容;
  498. rows = tuple(valueSheet.row_values(i))
  499. if rows[0].__len__() > 0:
  500. all_value_option_list.append(rows[0])
  501. print "all_value_option_list:", all_value_option_list
  502. return all_value_option_list
  503. def checkOptionPath(self, optionName):
  504. optionPath = ""
  505. optionPathResult = "Fail"
  506. path_params = self.get_option_paths(optionName)
  507. # 如果传入的option不存在则直接返回
  508. if path_params.__len__() <= 0:
  509. optionPathResult = "NoExit"
  510. optionPath = u"表格中不存在到达Option: %s 的路径" % str(optionName)
  511. return optionPath, optionPathResult
  512. else:
  513. optionPath = str(path_params)
  514. # 逆序路径key;
  515. revpp = reversed(path_params)
  516. level_count = 0
  517. for level in revpp:
  518. if str(level) == str(g_level[level_count]):
  519. level_count = level_count + 1
  520. else:
  521. # 如果执行路径不是按照First、Second......执行则返回执行出错
  522. return optionPath, optionPathResult
  523. optionPathResult = "Pass"
  524. return optionPath, optionPathResult
  525. def checkRunOptionPath(self, path_params):
  526. optionPathResult = "Fail"
  527. # 如果传入的option不存在则直接返回
  528. if path_params.__len__() <= 0:
  529. optionPathResult = "NoExit"
  530. return optionPathResult
  531. else:
  532. optionPath = str(path_params)
  533. # 逆序路径key;
  534. revpp = reversed(path_params)
  535. level_count = 0
  536. for level in revpp:
  537. if str(level) == str(g_level[level_count]):
  538. level_count = level_count + 1
  539. else:
  540. # 如果执行路径不是按照First、Second......执行则返回执行出错
  541. return optionPathResult
  542. optionPathResult = "Pass"
  543. return optionPathResult
  544. def getCheckOptionPathExcelData(self):
  545. checkOptionPathExcelData = []
  546. checkOptionPathExcelData_Fail = []
  547. checkOptionPathExcelData_NoExit = []
  548. checkOptionPathExcelData_Pass = []
  549. valueSheetOptionList = self.getValueSheetOptionList()
  550. for optionName in valueSheetOptionList:
  551. optionPath, optionPathResult = self.checkOptionPath(optionName)
  552. if str(optionPathResult) == "Fail":
  553. checkOptionPathExcelData_Fail.append([optionName, optionPathResult, optionPath])
  554. if str(optionPathResult) == "NoExit":
  555. checkOptionPathExcelData_NoExit.append([optionName, optionPathResult, optionPath])
  556. if str(optionPathResult) == "Pass":
  557. checkOptionPathExcelData_Pass.append([optionName, optionPathResult, optionPath])
  558. # checkOptionPathExcelData.append([optionName, optionPathResult, optionPath])
  559. checkOptionPathExcelData = checkOptionPathExcelData_Fail + checkOptionPathExcelData_NoExit + checkOptionPathExcelData_Pass
  560. return checkOptionPathExcelData
  561. def saveExcelData(self, checkOptionPathExcelData, repeatOptionList, all_UI_TestLanguagePath):
  562. # 设置字体
  563. font = xlwt.Font()
  564. font.bold = True
  565. # 设置边框
  566. borders = xlwt.Borders()
  567. borders.left = xlwt.Borders.THIN
  568. borders.right = xlwt.Borders.THIN
  569. borders.top = xlwt.Borders.THIN
  570. borders.bottom = xlwt.Borders.THIN
  571. # 设置居中
  572. alignment = xlwt.Alignment()
  573. # alignment.horz = xlwt.Alignment.HORZ_CENTER # 水平方向
  574. alignment.horz = xlwt.Alignment.HORZ_LEFT # 水平方向
  575. alignment.vert = xlwt.Alignment.VERT_TOP # 垂直方向
  576. alignment.wrap = 1
  577. # 设置背景颜色
  578. pattern = xlwt.Pattern()
  579. pattern.pattern = xlwt.Pattern.SOLID_PATTERN
  580. pattern.pattern_fore_colour = 3 # 背景颜色
  581. # 定义不同的excel style
  582. style1 = xlwt.XFStyle()
  583. style1.font = font
  584. style1.borders = borders
  585. style1.alignment = alignment
  586. style2 = xlwt.XFStyle()
  587. style2.borders = borders
  588. style2.alignment = alignment
  589. style = XFStyle()
  590. style.borders = borders
  591. style.alignment = alignment
  592. pattern = Pattern()
  593. pattern.pattern = Pattern.SOLID_PATTERN
  594. pattern.pattern_fore_colour = xlwt.Style.colour_map['red'] # 设置单元格背景色为黄色
  595. style.pattern = pattern
  596. # style_align = xlwt.easyxf('align: wrap on')
  597. if os.path.exists(all_UI_TestLanguagePath):
  598. try:
  599. os.remove(all_UI_TestLanguagePath)
  600. except Exception, e:
  601. print e
  602. return False
  603. # 创建工作簿;
  604. book = xlwt.Workbook(encoding='utf-8')
  605. # 创建sheet;
  606. sheet = book.add_sheet(u'到达Option路径检测', cell_overwrite_ok=True)
  607. # 设置表格自适应宽度的数组
  608. col_width = [2, 25, 15, 120]
  609. # 创建标题行;
  610. row0 = [u'序号', u'Option名称', u'检测到达路径结果', u'到达Option路径']
  611. for i in range(len(row0)):
  612. sheet.write(0, i, row0[i], style=style1)
  613. index = 1
  614. for checkdata in checkOptionPathExcelData:
  615. sheet.write(index, 0, index, style=style2)
  616. for j in range(len(checkdata)):
  617. # 如果结果不为Pass则在表格中标红
  618. if j == 1 and checkdata[j] != 'Pass':
  619. sheet.write(index, j + 1, checkdata[j], style=style)
  620. else:
  621. # if j == 1:
  622. # sheet.write(index, j + 1, checkdata[j], style=style_align)
  623. # else:
  624. sheet.write(index, j + 1, checkdata[j], style=style2)
  625. # if col_width[j] < self.len_byte(checkdata[j]):
  626. # col_width[j] = self.len_byte(checkdata[j])
  627. index += 1
  628. # 设置栏位宽度,栏位宽度小于10时候采用默认宽度
  629. for i in range(len(col_width)):
  630. if col_width[i] > 10:
  631. sheet.col(i).width = 256 * (col_width[i] + 1)
  632. sheet2 = book.add_sheet(u'Option查重', cell_overwrite_ok=True)
  633. # 设置表格自适应宽度的数组
  634. col_width2 = [2, 30, 30]
  635. # 创建标题行;
  636. row02 = [u'序号', u'重复Option名称', u'重复Option在源表格的sheet名称']
  637. for i in range(len(row02)):
  638. sheet2.write(0, i, row02[i], style=style1)
  639. index2 = 1
  640. for repeatOption in repeatOptionList:
  641. sheet2.write(index2, 0, index2, style=style2)
  642. for j in range(len(repeatOption)):
  643. sheet2.write(index2, j + 1, repeatOption[j], style=style2)
  644. index2 += 1
  645. # 设置栏位宽度,栏位宽度小于10时候采用默认宽度
  646. for i in range(len(col_width2)):
  647. if col_width2[i] > 10:
  648. sheet2.col(i).width = 256 * (col_width2[i] + 1)
  649. # 保存xls;
  650. try:
  651. book.save(all_UI_TestLanguagePath)
  652. return True
  653. except Exception, e:
  654. print e
  655. return False
  656. # 新增表格查询接口
  657. # # 获取表格Value层级中,value_name下包含的value列表
  658. # def getSubValueList(self, value_name):
  659. # valueList = []
  660. # if self.valueParams.values.has_key(value_name):
  661. # # print parser.valueParams.values["source"]
  662. # # print parser.valueParams.values["source"]["value"]
  663. # valueDictList = self.valueParams.values[value_name]["value"]
  664. # for valueDict in valueDictList:
  665. # valueList.append(valueDict["value"])
  666. # # print "valueList:", valueList, type(valueList)
  667. # return valueList
  668. # 获取表格Value层级中,value_name下包含的value列表
  669. def getSubValueList(self, value_name):
  670. valueList = []
  671. if self.valueParams.values.has_key(value_name):
  672. # print parser.valueParams.values["source"]
  673. # print parser.valueParams.values["source"]["value"]
  674. valueDictList = self.valueParams.values[value_name]["value"]
  675. for valueDict in valueDictList:
  676. valueList.append(valueDict["value"])
  677. # print "valueList:", valueList, type(valueList)
  678. if valueList.__len__() == 1 and "range(" in valueList[0]:
  679. value_for_ocr_low = str(valueList[0]).lower()
  680. str_num = value_for_ocr_low.replace("range", "")
  681. list_num = eval(str_num)
  682. min_num = list_num[0]
  683. max_num = list_num[1]
  684. return [min_num, max_num]
  685. return valueList
  686. # 获取表格First~Sixth层级中,parent下包含的option列表
  687. def getSubOptionList(self, parent):
  688. optionList = []
  689. paths = self.pathParams.paths
  690. for level in paths:
  691. if paths[level].has_key(parent):
  692. optionDictList = paths[level][parent]["value"]
  693. for optionDict in optionDictList:
  694. optionList.append(optionDict["option"])
  695. # print "optionList:", optionList, type(optionList)
  696. return optionList
  697. # 获取表格Value层级中,value_name下的value的ocr列表
  698. def getValueTextList(self, value_name, value):
  699. valueTextList = []
  700. if self.valueParams.values.has_key(value_name):
  701. # print parser.valueParams.values["source"]
  702. # print parser.valueParams.values["source"]["value"]
  703. valueDictList = self.valueParams.values[value_name]["value"]
  704. for valueDict in valueDictList:
  705. if valueDict["value"] == value:
  706. value_for_ocr = valueDict["value_for_ocr"]
  707. # print "value_for_ocr:", value_for_ocr, type(value_for_ocr)
  708. valueTextList = value_for_ocr.split(';')
  709. # print "valueTextList:", valueTextList, type(valueTextList)
  710. return valueTextList
  711. # 获取表格First~Sixth层级中,parent下的option的ocr列表
  712. def getOptionTextList(self, parent, option):
  713. optionTextList = []
  714. paths = self.pathParams.paths
  715. for level in paths:
  716. if paths[level].has_key(parent):
  717. optionDictList = paths[level][parent]["value"]
  718. for optionDict in optionDictList:
  719. if optionDict["option"] == option:
  720. option_for_ocr = optionDict["option_for_ocr"]
  721. # print "option_for_ocr:", option_for_ocr, type(option_for_ocr)
  722. optionTextList = option_for_ocr.split(';')
  723. # print "optionList:", optionTextList, type(optionTextList)
  724. return optionTextList
  725. if __name__ == "__main__":
  726. # path = r'E:\svn\SAT\项目资料\第四期-推广使用和画面检测\06-设计文档\01-蓝图设计\UiTree3.0方案\MenuTree_MS6586_EU.xls'
  727. # path = r'E:\SAT\resource\MenuTree\MS3663\DVB-T2 panasonic\MenuTree.xls'
  728. # path = r'E:/SAT/resource\MenuTree\MS6586\ATSC\MenuTree.xls'
  729. path = r'E:\SAT\resource\MenuTree\MS6586\ATSC\MenuTree_DVBT.xls'
  730. # path = r'E:\MenuTree.xls'
  731. # path = r'E:\SAT\resource\MenuTree\6586\MS6586_ATSC\MenuTree.xls'
  732. # path = getMenuTree3SelectedProjectCfgPath() + '\\MenuTree.xls'
  733. parser = CExcelParser(path)
  734. parser.read_excel()
  735. dataDict = parser.get_parent_ocr_dict("network")
  736. print "dataDict:", dataDict, type(dataDict)
  737. # parent = "source"
  738. # option = "atv"
  739. # parser.getOptionTextList(parent, option)
  740. # value_name = "source"
  741. # value = "atv"
  742. # parser.getValueTextList(value_name, value)
  743. # SubValueList1 = parser.getSubValueList("source")
  744. # SubValueList2 = parser.getSubValueList("backlight")
  745. # parent = "picture"
  746. # parent = "manual_tuning"
  747. # parser.getSubOptionList(parent)
  748. # optionList = []
  749. # paths = parser.pathParams.paths
  750. #
  751. # for level in paths:
  752. # if paths[level].has_key(parent):
  753. # optionDictList = paths[level][parent]["value"]
  754. # for optionDict in optionDictList:
  755. # optionList.append(optionDict["option"])
  756. # print "optionList:", optionList, type(optionList)
  757. # print parser.get_value("source", "")
  758. # checkOptionPathExcelData = parser.getCheckOptionPathExcelData()
  759. # print "checkOptionPathExcelData:", checkOptionPathExcelData
  760. # repeatOptionList = parser.getRepeatOptionList()
  761. # isSaveExcelTrue = parser.saveExcelData(checkOptionPathExcelData, repeatOptionList,
  762. # r'E:\MenuTree_Check_Option_Result.xls')
  763. # path = r'E:\SAT\resource\MenuTree\6586\MS6586_ATSC\MenuTree.xls'
  764. # path = getMenuTree3SelectedProjectCfgPath() + '\\MenuTree.xls'
  765. # print "isSaveExcelTrue:", isSaveExcelTrue
  766. # valueSheetOptionList = parser.getValueSheetOptionList()
  767. # repeatValueName_ValueSheet = parser.getRepeatValueName_ValueSheet()
  768. # repeatOption_otherSheet = parser.getRepeatOption_otherSheet()
  769. # optionPath, optionPathResult = parser.checkOptionPath("picture_preset11")
  770. # print "optionPath:", optionPath, type(optionPath)
  771. # print "optionPathResult:", optionPathResult, type(optionPathResult)
  772. # optionPath, optionPathResult = parser.checkOptionPath("instant_power_on")
  773. # print "optionPath:", optionPath, type(optionPath)
  774. # print "optionPathResult:", optionPathResult, type(optionPathResult)
  775. # path_params = parser.get_option_paths("picture_preset")
  776. # print "path_params:", path_params, type(path_params)
  777. # print "path_params[0][0]:",path_params[0][0]
  778. # print parser.get_option_paths('picture')
  779. # print parser.get_option('picture')
  780. # print parser.get_value('backlight',10)
  781. # print parser.get_parent_ocr_dict('source_hdmi1')
  782. # print parser.get_parent_ocr_list('channel')
  783. # print parser.get_parent_ocr_dict('channel')
  784. # print parser.get_parent_ocr_dict('other_setting')
  785. # print parser.get_parent_ocr_list('5khz')
  786. # print parser.get_parent_ocr_dict('picture_preset', True)
  787. # print parser.get_pair_values('picture_preset', False)
  788. # print parser.get_pair_values('source')
  789. # print parser.get_ocr_list('Second','picture','picture_preset')
  790. # vp, pp = parser.get_menu_paths("auto_volume_control", "off")
  791. # print vp
  792. # vp, pp = parser.get_menu_paths("picture_preset", "dynamic")
  793. # print vp
  794. # pp = parser.get_option_paths('picture_preset')
  795. # print pp.__len__(), pp[g_level[0]]
  796. # print "\r\nvp:", vp, type(vp)
  797. # print "\r\npp:", pp, type(pp)
  798. # # print json.dumps(pp)
  799. # revpp = reversed(pp)
  800. # for item in revpp:
  801. # print item, pp[item]
  802. # print 'value:\r\n'
  803. # print vp
  804. # # print "revpp:", revpp, type(revpp)