TExcelParser.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 xlsConst import xlsConst as xlsc
  10. from BaseLog import CBaseLog
  11. class CExcelParser(CBaseLog):
  12. def __init__(self, optionExcel, xls_path=None):
  13. self.__optionExcel = optionExcel
  14. if type(xls_path) == str:
  15. xls_path = xls_path.decode('utf-8')
  16. self.xls_path = xls_path
  17. def read_excel(self, path=None):
  18. if path is not None:
  19. if type(path) == str:
  20. path = path.decode('utf-8')
  21. self.xls_path = path
  22. if not os.path.exists(self.xls_path):
  23. return
  24. # 打开文件;
  25. wb = xlrd.open_workbook(filename=self.xls_path)
  26. if wb is None:
  27. return
  28. # 获取所有sheet;
  29. sheet = None
  30. for sh_name in wb.sheet_names():
  31. sheet = wb.sheet_by_name(sh_name)
  32. self.parse_excel(sheet, False)
  33. # endfun
  34. def parse_excel(self, sheet, bpath=True):
  35. pKeys = sheet.row_values(0)
  36. self.info(u"params sheet %s keys: %s" % (sheet.name, str(pKeys)))
  37. parentName = None
  38. if u"Value" == sheet.name:
  39. for i in range(1, sheet.nrows):
  40. # 获取每行内容;
  41. oneRow = tuple(sheet.row_values(i))
  42. rowDict = {}
  43. for index in range(oneRow.__len__()):
  44. item = oneRow[index]
  45. rowDict[pKeys[index]] = item
  46. if rowDict[xlsc.value_name] is None or rowDict[xlsc.value_name].__len__() < 1:
  47. if parentName is None:
  48. self.error(u"Level %s error row %s:" % (sheet.name, str(oneRow)))
  49. break
  50. else:
  51. rowDict[xlsc.value_name] = parentName
  52. self.__optionExcel.valueParams.addOption(rowDict)
  53. elif rowDict[xlsc.value_name].__len__() > 0:
  54. parentName = rowDict[xlsc.value_name]
  55. self.__optionExcel.valueParams.addParent(rowDict)
  56. elif "Dialog" == sheet.name:
  57. self.parsePath(sheet, pKeys, self.__optionExcel.dialogParams)
  58. else: # 路径;
  59. self.parsePath(sheet, pKeys, self.__optionExcel.pathParams)
  60. def parsePath(self, sheet, pKeys, treeParams):
  61. parentName = None
  62. for i in range(1, sheet.nrows):
  63. # 获取每行内容;
  64. oneRow = tuple(sheet.row_values(i))
  65. rowDict = {}
  66. for index in range(oneRow.__len__()):
  67. item = oneRow[index]
  68. rowDict[pKeys[index]] = item
  69. if rowDict[xlsc.parent] is None or rowDict[xlsc.parent].__len__() < 1:
  70. if parentName is None:
  71. self.error(u"Level %s error row %s:" % (sheet.name, str(oneRow)))
  72. continue
  73. else:
  74. rowDict[xlsc.parent] = parentName
  75. treeParams.addOption(sheet.name, rowDict)
  76. elif rowDict[xlsc.FLevel_Params[0]].__len__() > 0:
  77. parentName = rowDict[xlsc.parent]
  78. treeParams.addParent(sheet.name, rowDict)
  79. # endfun
  80. def saveExcelData(self, checkOptionPathExcelData, repeatOptionList, all_UI_TestLanguagePath):
  81. # 设置字体
  82. font = xlwt.Font()
  83. font.bold = True
  84. # 设置边框
  85. borders = xlwt.Borders()
  86. borders.left = xlwt.Borders.THIN
  87. borders.right = xlwt.Borders.THIN
  88. borders.top = xlwt.Borders.THIN
  89. borders.bottom = xlwt.Borders.THIN
  90. # 设置居中
  91. alignment = xlwt.Alignment()
  92. # alignment.horz = xlwt.Alignment.HORZ_CENTER # 水平方向
  93. alignment.horz = xlwt.Alignment.HORZ_LEFT # 水平方向
  94. alignment.vert = xlwt.Alignment.VERT_TOP # 垂直方向
  95. alignment.wrap = 1
  96. # 设置背景颜色
  97. pattern = xlwt.Pattern()
  98. pattern.pattern = xlwt.Pattern.SOLID_PATTERN
  99. pattern.pattern_fore_colour = 3 # 背景颜色
  100. # 定义不同的excel style
  101. style1 = xlwt.XFStyle()
  102. style1.font = font
  103. style1.borders = borders
  104. style1.alignment = alignment
  105. style2 = xlwt.XFStyle()
  106. style2.borders = borders
  107. style2.alignment = alignment
  108. style = XFStyle()
  109. style.borders = borders
  110. style.alignment = alignment
  111. pattern = Pattern()
  112. pattern.pattern = Pattern.SOLID_PATTERN
  113. pattern.pattern_fore_colour = xlwt.Style.colour_map['red'] # 设置单元格背景色为黄色
  114. style.pattern = pattern
  115. # style_align = xlwt.easyxf('align: wrap on')
  116. if os.path.exists(all_UI_TestLanguagePath):
  117. try:
  118. os.remove(all_UI_TestLanguagePath)
  119. except Exception, e:
  120. print e
  121. return False
  122. # 创建工作簿;
  123. book = xlwt.Workbook(encoding='utf-8')
  124. # 创建sheet;
  125. sheet = book.add_sheet(u'到达Option路径检测', cell_overwrite_ok=True)
  126. # 设置表格自适应宽度的数组
  127. col_width = [2, 25, 15, 120]
  128. # 创建标题行;
  129. row0 = [u'序号', u'Option名称', u'检测到达路径结果', u'到达Option路径']
  130. for i in range(len(row0)):
  131. sheet.write(0, i, row0[i], style=style1)
  132. index = 1
  133. for checkdata in checkOptionPathExcelData:
  134. sheet.write(index, 0, index, style=style2)
  135. for j in range(len(checkdata)):
  136. # 如果结果不为Pass则在表格中标红
  137. if j == 1 and checkdata[j] != 'Pass':
  138. sheet.write(index, j + 1, checkdata[j], style=style)
  139. else:
  140. # if j == 1:
  141. # sheet.write(index, j + 1, checkdata[j], style=style_align)
  142. # else:
  143. sheet.write(index, j + 1, checkdata[j], style=style2)
  144. # if col_width[j] < self.len_byte(checkdata[j]):
  145. # col_width[j] = self.len_byte(checkdata[j])
  146. index += 1
  147. # 设置栏位宽度,栏位宽度小于10时候采用默认宽度
  148. for i in range(len(col_width)):
  149. if col_width[i] > 10:
  150. sheet.col(i).width = 256 * (col_width[i] + 1)
  151. sheet2 = book.add_sheet(u'Option查重', cell_overwrite_ok=True)
  152. # 设置表格自适应宽度的数组
  153. col_width2 = [2, 30, 30]
  154. # 创建标题行;
  155. row02 = [u'序号', u'重复Option名称', u'重复Option在源表格的sheet名称']
  156. for i in range(len(row02)):
  157. sheet2.write(0, i, row02[i], style=style1)
  158. index2 = 1
  159. for repeatOption in repeatOptionList:
  160. sheet2.write(index2, 0, index2, style=style2)
  161. for j in range(len(repeatOption)):
  162. sheet2.write(index2, j + 1, repeatOption[j], style=style2)
  163. index2 += 1
  164. # 设置栏位宽度,栏位宽度小于10时候采用默认宽度
  165. for i in range(len(col_width2)):
  166. if col_width2[i] > 10:
  167. sheet2.col(i).width = 256 * (col_width2[i] + 1)
  168. # 保存xls;
  169. try:
  170. book.save(all_UI_TestLanguagePath)
  171. return True
  172. except Exception, e:
  173. print e
  174. return False
  175. # 新增表格查询接口
  176. # # 获取表格Value层级中,value_name下包含的value列表
  177. # def getSubValueList(self, value_name):
  178. # valueList = []
  179. # if self.valueParams.values.has_key(value_name):
  180. # # print parser.valueParams.values["source"]
  181. # # print parser.valueParams.values["source"]["value"]
  182. # valueDictList = self.valueParams.values[value_name]["value"]
  183. # for valueDict in valueDictList:
  184. # valueList.append(valueDict["value"])
  185. # # print "valueList:", valueList, type(valueList)
  186. # return valueList
  187. if __name__ == "__main__":
  188. pass