使用Python將xmind腦圖轉(zhuǎn)成excel用例的實(shí)現(xiàn)代碼(一)
最近接到一個領(lǐng)導(dǎo)需求,將xmind腦圖直接轉(zhuǎn)成可以導(dǎo)入的excel用例,并且轉(zhuǎn)換成gui可執(zhí)行的exe文件,方便他人使用。
因?yàn)閷ython比較熟悉,所以就想使用Python3來實(shí)現(xiàn)這個功能,先理一下思路,首先要將xmind轉(zhuǎn)換成Python可用的數(shù)據(jù)格式,正好找到了一個xmindparser庫可以做這個事情,然后就好辦了,用xlwt庫寫成xls文件,再用Python自帶的gui庫 tkinter來寫 gui界面,最后用pyinstaller來打包。
計(jì)劃分兩個py文件來寫,一個文件寫excel ,一個文件寫gui界面,因?yàn)樾枰獙?dǎo)入的字段比較多,而且導(dǎo)入的格式有嚴(yán)格的要求,所以寫excel的邏輯還是有點(diǎn)復(fù)雜的,所以我們確定xmind的文件有相應(yīng)的格式來寫,測試編號 測試需求 測試用例標(biāo)題 測試用例執(zhí)行步驟 測試用例預(yù)期結(jié)果 測試用例預(yù)置條件都要按照相應(yīng)的格式來。格式如圖
通過xmindparser解析出來的格式是這樣的,是Python的字典加列表的格式
from xmindparser import xmind_to_dictimport reimport xlwtclass xmind_to_xls(): def xmind_num(self,value): '''獲取xmind標(biāo)題個數(shù)''' try: return len(value[’topics’]) except KeyError: return 0 def xmind_title(self,value): '''獲取xmind標(biāo)題內(nèi)容''' return value[’title’] def xmind_cat(self,filename): ’’’調(diào)試函數(shù),打印內(nèi)容用的’’’ self.out = xmind_to_dict(filename) self.story = self.out[0][’topic’][’topics’] self.num=len(self.story) print(self.out) print(self.out[0][’topic’][’title’]) return self.story,self.num def write_excel(self,xmind_file,servicename=’’,editionname=’’,performer=’’): ’’’生成excel文件函數(shù)’’’ self.f=xlwt.Workbook() self.sheet1 =self.f.add_sheet(’sheet1’,cell_overwrite_ok=True) self.row0 = ['storyid', ’需求名稱’, ’測試用例名稱’, ’執(zhí)行步驟’, ’預(yù)期結(jié)果’, ’服務(wù)名稱’, ’版本’, ’執(zhí)行人員’] #生成第一行 for i in range(0,len(self.row0)): self.sheet1.write(0,i,self.row0[i]) self.out = xmind_to_dict(xmind_file) self.xls_name=self.out[0][’topic’][’title’] self.story = self.out[0][’topic’][’topics’] self.storynum = len(self.story) j=1 #用例計(jì)算器 z = 0 # 用例結(jié)果數(shù)計(jì)數(shù)器 for i in range(0, self.storynum): self.storyname = self.story[i][’title’] print(self.storyname) self.regex_str = '.*[[【](.+?)[]】].*' self.storyid_reg = re.match(self.regex_str, self.storyname) if self.storyid_reg: self.storyid=self.storyid_reg.group(1)#正則取出用例編號 #print(self.storyid_reg.group(1)) self.testcase_num=self.xmind_num(self.story[i][’topics’][0]) for k in range(0,self.testcase_num): self.testcase=self.story[i][’topics’][0][’topics’][k] self.testcase_name =self.xmind_title(self.testcase) self.testcase_stepnum=self.xmind_num(self.testcase) #每個用例的步驟數(shù)量 self.sheet1.write(k + i + z + j, 2, self.testcase_name) self.sheet1.write(k + i + z + j, 0, self.storyid) self.sheet1.write(k + i + z + j, 1, self.storyname) self.sheet1.write(k + i + z + j, 5, servicename) self.sheet1.write(k + i + z + j, 6, editionname) self.sheet1.write(k + i + z + j, 7, performer) for x in range(0,self.testcase_stepnum): self.testcase_step=self.testcase[’topics’][x] self.teststep_title=self.xmind_title(self.testcase_step) #用例步驟名稱 self.teststep_num=self.xmind_num(self.testcase_step) #用例步驟個數(shù) if self.teststep_num != 0: for y in range(0,self.teststep_num): self.test_results=self.testcase_step[’topics’][y] self.test_result=self.xmind_title(self.test_results)#用例結(jié)果 self.sheet1.write(k + i + z + j+y+1, 3, self.teststep_title) self.sheet1.write(k + i + z + j + y+1, 4, self.test_result) z = z + y+1 else: self.test_result=’ ’ self.sheet1.write(k + i + z + j+1 , 3, self.teststep_title) self.sheet1.write(k + i + z + j+1 , 4, self.test_result) z = z + 1 j=j+k self.f.save(self.xls_name+’.xls’) #xls名稱取xmind主題名稱if __name__ == ’__main__’: xmind_file = 'C:UsersAdministratorDesktop版本測試.xmind' # xmind文件 servicename=’aa’ editionname=’bb’ performer=’cc’ #xmind_to_xls().write_excel(xmind_file,servicename,editionname,performer) xmind_to_xls().xmind_cat(xmind_file)
到此這篇關(guān)于使用Python將xmind腦圖轉(zhuǎn)成excel用例的實(shí)現(xiàn)代碼(一)的文章就介紹到這了,更多相關(guān)Python xmind轉(zhuǎn)excel用例內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. HTML <!DOCTYPE> 標(biāo)簽2. 5個HTML5的常用本地存儲方式詳解與介紹3. asp在iis7報(bào)錯行號不準(zhǔn)問題的解決方法4. asp批量添加修改刪除操作示例代碼5. 告別AJAX實(shí)現(xiàn)無刷新提交表單6. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法7. HTML中的XML數(shù)據(jù)島記錄編輯與添加8. CSS代碼檢查工具stylelint的使用方法詳解9. 三個不常見的 HTML5 實(shí)用新特性簡介10. 原生js XMLhttprequest請求onreadystatechange執(zhí)行兩次的解決
