TExcelParser.py 7.9 KB

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