TExcelParser.py 8.0 KB

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