生活随笔
收集整理的這篇文章主要介紹了
xmind转excel脚本(简化版)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
前言
xmind轉excel(簡化版)由于測試過程中需要編寫用例點和測試用例,每次都是測試點寫完,還得將測試點復制到excel中,基于這種前提下,寫了個簡化版的xmind工具
思路
1.獲取xmind中數據,調用“xmind_to_dict”方法獲取到dict類型的xmind數據
2.通過dict的操作獲取到從測試點數據,并封裝成list,其中嚴重性和預期結果相同
3.在通過openpyxl庫,調用“cell"方法,根據指定列去寫入數據(其中由于用例模板中嚴重性是字母表示,而xmind中獲取的是阿拉伯數字,所以嚴重在寫入時要進行一次轉換)
二、使用步驟
1.使用的庫
openpyxl
xmindparser
import openpyxl as openpyxl
from xmindparser import xmind_to_dict
#設置excel列和嚴重性全局變量global title_excel
title_excel = {'A':1,'C':3,'D':4,'G':7}
global Priority
Priority = {1:'A',2:'B',3:'C',4:'D'}class xmindParserTool():def __init__(self,xmind_path):self.xmind_path = xmind_pathself.topics = xmind_to_dict(self.xmind_path)[0]['topic']#獲取數據一級節點def getOneTopics(self):topics = self.topics.get('topics')return topics#獲取測試點@propertydef testPoint(self):one_topics = self.getOneTopics()title =[]for j in range(len(one_topics)):topic =one_topics[j].get('topics')for i in range(len(topic)):if topic[i]['title'] !=None:title.append(topic[i]['title'])else:title.append('無測試點')return title#獲取嚴重等級數據@propertydef getPriority(self):one_topics = self.getOneTopics()priority = []for j in range(len(one_topics)):topic = one_topics[j].get('topics')for i in range(len(topic)):# 截取字符串,獲取嚴重等級priority_data = topic[i].get('makers')if priority_data !=None:priority_split = priority_data[0].split('-')priority.append(priority_split[1])else:priority.append('4')return priority#獲取用例預期結果數據@propertydef expectedResults(self):one_topics = self.getOneTopics()title =[]for i in range(len(one_topics)):expected_topics = one_topics[i]['topics']for j in range(len(expected_topics)):try:expected_topics_inner = expected_topics[j]['topics']except KeyError:title.append('無預期結果')else:for k in range(len(expected_topics_inner)):title.append(expected_topics_inner[k]['title'])return title# if __name__ == '__main__':
# xx = xmindParserTool(r'C:\Users\Administrator\Desktop\自動化平臺需求-V4.4-登錄信息作用域改造.xmind')
# print(xx.getPriority)class writeToExcel(xmindParserTool):def __init__(self, xmind_path,excel_path):self.xmind = xmindParserTool(xmind_path)self.path = excel_pathself.excel = openpyxl.load_workbook(self.path)self.active = self.excel.worksheets[0]#指定列列,逐行寫入數據def writeData(self):#測試點數據gTT_data=self.xmind.testPoint#預期結果gFT_data=self.xmind.expectedResults#嚴重等級gP_data=self.xmind.getPriorityfor i in range(len(gTT_data)):for j in range(len(gTT_data[i])):self.active.cell(row=i + 2, column=title_excel.get('A')).value = gTT_data[i]for i in range(len(gFT_data)):for j in range(len(gFT_data[i])):self.active.cell(row=i + 2, column=title_excel.get('C')).value = gFT_data[i]for i in range(len(gP_data)):self.active.cell(row=i + 2, column=title_excel.get('D')).value = Priority.get(int(gP_data[i]))if Priority.get(int(gP_data[i])) == 'A':self.active.cell(row=i + 2, column=title_excel.get('G')).value = '是'self.excel.save(self.path)if __name__ == '__main__':
#在xmind和excel的format中輸入對應文件名xmind = r'C:\Users\Administrator\Desktop\{0}.xmind'.format('xmind_name')excel = r'C:\Users\Administrator\Desktop\{0}.xlsx'.format('excel_name')ww = writeToExcel(xmind,excel)ww.writeData()
總結
1.python初級小白,代碼寫的比較low,大神們看看就好,主要是記錄下自己學習過程中的成果,輕噴。
2.通過這個腳本也一定程度減輕了自己日常工作的重復性勞作,提升了自己效率,
總結
以上是生活随笔為你收集整理的xmind转excel脚本(简化版)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。