#coding = utf-8 #from __future__ import division from xlwt import * """ ------Color----- #这些都可以从源码里面找到。 aqua 0x31 black 0x08 blue 0x0C blue_gray 0x36 bright_green 0x0B brown 0x3C coral 0x1D cyan_ega 0x0F dark_blue 0x12 dark_blue_ega 0x12 dark_green 0x3A dark_green_ega 0x11 dark_purple 0x1C dark_red 0x10 dark_red_ega 0x10 dark_teal 0x38 dark_yellow 0x13 gold 0x33 gray_ega 0x17 gray25 0x16 gray40 0x37 gray50 0x17 gray80 0x3F green 0x11 ice_blue 0x1F indigo 0x3E ivory 0x1A lavender 0x2E light_blue 0x30 light_green 0x2A light_orange 0x34 light_turquoise 0x29 light_yellow 0x2B lime 0x32 magenta_ega 0x0E ocean_blue 0x1E olive_ega 0x13 olive_green 0x3B orange 0x35 pale_blue 0x2C periwinkle 0x18 pink 0x0E plum 0x3D purple_ega 0x14 red 0x0A rose 0x2D sea_green 0x39 silver_ega 0x16 sky_blue 0x28 tan 0x2F teal 0x15 teal_ega 0x15 turquoise 0x0F violet 0x14 white 0x09 yellow 0x0D """ def cellStyle(**keys): #这个之前我是每一类单元格都用了一个函数来处理,后来发现如果要求的style很多,怎么搞? 于是就采用字典来预先定义好,最后通过**keys来传需要修改的参数就OK了。 style_dict = { "font_name":'Arial', "font_colour_index":0x8, "font_bold":False, "borders_left":1, "borders_right":1, "borders_top":1, "borders_bottom":1, "borders_left_colour":0x08, "borders_right_colour":0x08, "borders_top_colour": 0x08, "borders_bottom_colour":0x08, "alignment_horz":0, "alignment_vert":2, "pattern_fore_colour":31 } if keys == None: pass else: for key in keys: style_dict[key] = keys[key] ##下面这些实例和属性除了从网上参考,还有就是xlwt库中的源码了.比如formatting.py,style.py等。 fnt = Font() borders = Borders() fnt.name = style_dict["font_name"] fnt.colour_index = style_dict["font_colour_index"] fnt.bold = style_dict["font_bold"] borders = Borders() borders.left = style_dict["borders_left"] borders.right = style_dict["borders_right"] borders.top = style_dict["borders_top"] borders.bottom = style_dict["borders_bottom"] borders.left_colour = style_dict["borders_left_colour"] borders.right_colour = style_dict["borders_right_colour"] borders.top_colour = style_dict["borders_top_colour"] borders.bottom_colour = style_dict["borders_bottom_colour"] alignment = Alignment() alignment.horz = style_dict["alignment_horz"] #HORZ_CENTER = 0x02 alignment.vert = style_dict["alignment_vert"] #VERT_CENTER = 0x01 pattern = Pattern() pattern.pattern = Pattern.SOLID_PATTERN pattern.pattern_fore_colour = style_dict["pattern_fore_colour"] # "pattern.pattern = xlwt.Pattern.SOLID_PATTERN # May be: NO_PATTERN, SOLID_PATTERN, or 0x00 through 0x12 # pattern.pattern_fore_colour = 5 # May be: 8 through 63. 0 = Black, 1 = White, 2 = Red, 3 = Green, 4 = Blue, 5 = Yellow, 6 = Magenta, 7 = Cyan, 16 = Maroon, 17 = Dark Green, 18 = Dark Blue, 19 = Dark Yellow , almost brown), 20 = Dark Magenta, 21 = Teal, 22 = Light Gray, 23 = Dark Gray, the list goes on..." style = XFStyle() style.font = fnt style.borders = borders style.alignment =alignment style.pattern = pattern style.num_format_str = "M/D/YY h:mm" return style #***********************EXCEL STYLE*************************************** #***********************EXCEL Generate*************************************** def generateXLS(fbDict): wb = Workbook() ws0 = wb.add_sheet('QC Report', cell_overwrite_ok=True) #*****************************Generate Title************************************* title = ["FB","Feature","Case Name","Status","Responsible Tester","Release","SW Build","Priority","ExecDate","Location","Organization","Comments"] for col_title in range(0,len(title)): style = cellStyle(pattern_fore_colour=18,font_colour_index=13,font_bold=True,alignment_horz=2,alignment_vert=1) ws0.write(0, col_title, str(title[col_title]), style) col_title += 1 #********************************************************************************* #********************************************************************************* fbDict.keys().sort() for fblist in fbDict.keys(): # print "fblist",fblist # "[FB1311]" featuredict = fbDict[fblist] # featuredict = {"Feature":LIST} # print "featuredict",featuredict #******************************* featuredict.keys().sort() start_row = 1 end_row = 0 for Feature in featuredict.keys(): # print featuredict.keys() #featuredict.keys() = ["Feature","RP00539_6B_1"] #featuredict.keys().sort() is the feature, this is to traversal how many feature instance_list = featuredict[Feature] #instance_list = ["instance|owner|status|date|comments|build","instance|owner|status|date|comments|build"] # print "Feature:",Feature # print instance_list #int lenOfInstancelist = len(instance_list) # lenOfInstancelist = 20,get the count of instance count end_row += lenOfInstancelist #print "end_row",end_row #wirte the RP00539_6B_1 wirteFeature(ws0,start_row,end_row,Feature) #print "lenOfInstancelist:",lenOfInstancelist #******************Instance Info*************************** for instance in instance_list: # print instance #caseName = instance[1] # print "instance_data",instance_data #print "start_row",start_row wirteInstance(ws0,start_row,instance) start_row += 1 #********************************************************* #********************************** wirteFB(ws0,end_row,fblist) wb.save('QC Report.xls') def wirteInstance(ws0,instance_row,instance): """Write Instance name, tester, statues and so on.""" # print "wirteInstance:",instance #ws0.write(start_row, 3, "instance[i]", Style2()) for i in range(0,len(instance)): #column = i+2 if instance[i]!=None: #这里是对case执行结果判断是否是failed,如果是,就将单元格渲染为红色。 if str(instance[i])=="Failed": ws0.write(instance_row, i+2, str(instance[i]), cellStyle(pattern_fore_colour=10)) else: ws0.write(instance_row, i+2, str(instance[i]), cellStyle()) if int(1+len(str(instance[i])=="))*256 <3333: #这里计算单元格中写的字符串长度如果小于1英寸就不处理,如果大于,就将单元格拉长.因为我实在找不到如何自适应单元格大小。目前不清楚怎么改进这里 pass else: ws0.col(i+2).width = int(1+len(str(instance[i]))*256) else: if str(instance[1]) == "Passed" and i==9: ws0.write(instance_row, i+2, " ", cellStyle()) else: ws0.write(instance_row, i+2, " ", cellStyle(pattern_fore_colour=10)) print "Write One case Info to Excel.." def wirteFeature(ws0,start_row,end_row,Feature): """Write Feature Name: RP00539_6b_1.""" style = cellStyle(font_bold=True,alignment_horz=2,alignment_vert=1) ws0.write_merge(start_row, end_row, 1, 1,Feature, style) ws0.col(1).width = int(1+len(Feature)*256) # if len(Feature)/12.0 < 1: # pass # else: # ws0.col(1).width = int(len(Feature)/12.0*3333) def wirteFB(ws0,end_row,fblist): """Write Feature Name: FB1311.""" style = cellStyle(font_bold=True,alignment_horz=2,alignment_vert=1) ws0.write_merge(1, end_row, 0, 0,fblist, style) ws0.col(0).width = int(1+len(fblist)*256) # if len(fblist)/12.0 < 1: # pass # else: # ws0.col(0).width = len(fblist)/12 *3333 基本上WriteEXCEL.py就OK了。 再将QC的内容保存为一个字典就行了。我保存的是:fbDict = {"FB1311":{"TestSet":["Case Name","Status","Responsible Tester","Release","SW Build","Priority","ExecDate","Location","Organization","Comments"]}} 看起来比较复杂,哈哈,实际上我比较喜欢用这种嵌套方式,因为源于QC中,一次性,要检查的TS比较多,所以做了列表方式。最后返回的结果组合我字典。这样方便接下来的后续处理。 补充上最后的main.py,执行这个脚本就能生成所需要的excel文件。 关于这里import的GetQCObject.py实际上就是上一篇关于获取QC testplan改出来的 #coding=utf-8 ''' Created on 2014年1月6日 @author: chenrxia ''' from WriteEXCEL import * from GetQCObject import * TEST_SET_PATH = r"Root\Test Sets\ReleaseQ1304\FB1311\New Feature" TEST_SET_NAME = ["RP000539_6b_1","RP000539_6a_1","RP000539_6b_2","RP000539_6a_2"] INSTANCE_INFO=True RUN_INFO=False qcServer = "https://10.135.55.13/qcbin" qcUser = "user" qcPassword = "passwd" qcDomain = "MBB_BTS" qcProject = "RP_BTS_IV" tsDict ={} qcConn = get_QCConnection(qcServer,qcUser,qcPassword,qcDomain,qcProject) for tsName in TEST_SET_NAME: #print tsName TestSetList = getInstance(qcConn,TEST_SET_PATH,tsName) InstanceList = lastInfoOfCase(TestSetList,InstanceIfo=INSTANCE_INFO,runInfo=RUN_INFO) #--+========================================================================== tsDict[tsName] =InstanceList fbDict = {"FB1311":tsDict} generateXLS(fbDict) put_QCConnection(qcConn) raw_input("Finished!")
最终有3个脚本,GetQCObject.py 获取QC信息WriteEXCEL.py写excel文件,和main.py最终的调用执行入口. 初学编程,组织的比较乱,请多多指教.