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_path
        self.topics = xmind_to_dict(self.xmind_path)[0]['topic']
    #获取数据一级节点
    def getOneTopics(self):
        topics = self.topics.get('topics')
        return topics


    #获取测试点
    @property
    def 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
    #获取严重等级数据
    @property
    def 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

    #获取用例预期结果数据
    @property
    def 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_path
        self.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.getPriority

        for 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.通过这个脚本也一定程度减轻了自己日常工作的重复性劳作,提升了自己效率,

你可能感兴趣的:(#,python小工具,python)