1 #coding:utf-8
2 importos3 importsys4 reload(sys)5 sys.setdefaultencoding("utf-8")6
7 from easy_excel importeasy_excel8 classoperate():9 def __init__(self, ExcelFileName, SheetName):10 self.excelFile = ExcelFileName + '.xls'
11 self.excelSheet =SheetName12 self.temp =easy_excel(self.excelFile)13 self.dic_testlink ={}14 self.row_flag = 3
15 self.testsuite = self.temp.getCell(self.excelSheet, 2, 1)16 self.dic_testlink[self.testsuite] = {"node_order": "13", "details": "", "testcase": []}17 self.content = ""
18 self.content_list =[]19
20 defxlsx_to_dic(self, SheetName):21 whileTrue:22 #print 'loop1'
23 #list_testcase = dic_testlink[testsuite].["testcase"]
24
25 testcase = {"name": "", "node_order": "100", "externalid": "", "version": "1", "summary": "",26 "preconditions": "", "execution_type": "1", "importance": "3", "steps": [], "keywords": "P1"}27 testcase["name"] = self.temp.getCell(self.excelSheet, self.row_flag, 1)28 testcase["summary"] = self.temp.getCell(self.excelSheet, self.row_flag, 3)29 testcase["preconditions"] = self.temp.getCell(self.excelSheet, self.row_flag, 4)30 execution_type = self.temp.getCell(self.excelSheet, self.row_flag, 7)31 if execution_type == "自动":32 testcase["execution_type"] = 2
33 #print self.temp.getCell('Sheet1',self.row_flag,3)
34 step_number = 1
35 testcase["keywords"] = self.temp.getCell(self.excelSheet, self.row_flag, 2)36 #print testcase["keywords"]
37 whileTrue:38 #print 'loop2'
39 step = {"step_number": "", "actions": "", "expectedresults": "", "execution_type": ""}40 step["step_number"] =step_number41 step["actions"] = self.temp.getCell(self.excelSheet, self.row_flag, 5)42 step["expectedresults"] = self.temp.getCell(self.excelSheet, self.row_flag, 6)43 testcase["steps"].append(step)44 step_number += 1
45 self.row_flag += 1
46 if self.temp.getCell(self.excelSheet, self.row_flag, 1) is not None or self.temp.getCell(self.excelSheet, self.row_flag, 5) isNone:47 break
48 #print testcase
49
50 self.dic_testlink[self.testsuite]["testcase"].append(testcase)51 #print self.row_flag
52 if self.temp.getCell(self.excelSheet, self.row_flag, 5) is None and self.temp.getCell(self.excelSheet, self.row_flag + 1, 5) isNone:53 break
54 self.temp.close()55 #print self.dic_testlink
56
57 def content_to_xml(self, key, value=None):58 if key == 'step_number' or key == 'execution_type' or key == 'node_order' or key == 'externalid' or key == 'version' or key == 'importance':59 return "" + str(key) + ">"
60 elif key == 'actions' or key == 'expectedresults' or key == 'summary' or key == 'preconditions':61 return "" + str(value) + "
]]>" + str(key) + ">"62 elif key == 'keywords':63 return ''
64 elif key == 'name':65 return ''
66 else:67 return '##########'
68
69 defdic_to_xml(self, ExcelFileName, SheetName):70 testcase_list = self.dic_testlink[self.testsuite]["testcase"]71 for testcase intestcase_list:72 for step in testcase["steps"]:73 self.content += ""
74 self.content += self.content_to_xml("step_number", step["step_number"])75 self.content += self.content_to_xml("actions", step["actions"])76 self.content += self.content_to_xml("expectedresults", step["expectedresults"])77 self.content += self.content_to_xml("execution_type", step["execution_type"])78 self.content += ""
79 self.content = "" + self.content + ""
80 self.content = self.content_to_xml("importance", testcase["importance"]) +self.content81 self.content = self.content_to_xml("execution_type", testcase["execution_type"]) +self.content82 self.content = self.content_to_xml("preconditions", testcase["preconditions"]) +self.content83 self.content = self.content_to_xml("summary", testcase["summary"]) +self.content84 self.content = self.content_to_xml("version", testcase["version"]) +self.content85 self.content = self.content_to_xml("externalid", testcase["externalid"]) +self.content86 self.content = self.content_to_xml("node_order", testcase["node_order"]) +self.content87 self.content = self.content + self.content_to_xml("keywords", testcase["keywords"])88 self.content = self.content_to_xml("name", testcase["name"]) +self.content89 self.content = self.content + ""
90 self.content_list.append(self.content)91 self.content = ""
92 self.content = "".join(self.content_list)93 self.content = '' + self.content + ""
94 self.content = '<?xml version="1.0" encoding="UTF-8"?>' +self.content95 self.write_to_file(ExcelFileName, SheetName)96
97 defwrite_to_file(self, ExcelFileName, SheetName):98 xmlFileName = ExcelFileName + '_' + SheetName + '.xml'
99 cp = open(xmlFileName, "w")100 cp.write(self.content)101 cp.close()102
103 if __name__ == "__main__":104
105 fileName = raw_input('enter excel name:')106 sheetName = raw_input('enter sheet name:')107 sheetList = sheetName.split(" ")108 for sheetName insheetList:109 test =operate(fileName, sheetName)110 test.xlsx_to_dic(sheetName)111 test.dic_to_xml(fileName, sheetName)112 print "Convert success!"
113 os.system('pause')