编程语言
首页 > 编程语言> > python实现xmind8转excel

python实现xmind8转excel

作者:互联网

python实现xmind8转excel

 

 

 

 

使用方法说明:

1、xmind上的一条用例的用例步骤和预期结果必须是要写在一行,切勿分多行描述步骤或预期结果,如果有前置条件的,需要标记蓝色感叹号且以“前置条件”4个字开头,如下图:

 

 

 

代码展示:

import xmindparser
import xlwt, xlrd
from xlutils.copy import copy
from xmindparser import xmind_to_dict
import time
a = time.time()
timestp=str(a).split('.')[0]

# xmindparser配置
xmindparser.config = {
            'showTopicId': False, # 是否展示主题ID
            'hideEmptyValue': True # 是否隐藏空值
        }

filePath = '测试.xmind'

def traversal_xmind(root, rootstring, lisitcontainer):
    """
    功能:递归dictionary文件得到容易写入Excel形式的格式。
    注意:rootstring都用str来处理中文字符
    @param root: 将xmind处理后的dictionary文件
    @param rootstring: xmind根标题
    """
    if isinstance(root, dict):
        if 'title' in root.keys() and 'topics' in root.keys():
            traversal_xmind(root['topics'], str(rootstring), lisitcontainer)
        if 'title' in root.keys() and 'topics' not in root.keys():
            traversal_xmind(root['title'], str(rootstring), lisitcontainer)
    elif isinstance(root, list):
        for sonroot in root:
            # traversal_xmind(sonroot, str(rootstring) + "&" + sonroot['title'], lisitcontainer)
            #print('sonroot:',sonroot)
            if 'makers' in sonroot and 'title'  in sonroot :
                # print('sonroot:',sonroot)
                # print('root[1]:', root[1])
                if 'topics' in root[1]:
                    traversal_xmind(sonroot, str(rootstring) + "&" + sonroot['title']+ "&"+root[1]['title']+ "&"+root[1]['topics'][0]['title'], lisitcontainer)
            else:
                traversal_xmind(sonroot, str(rootstring) + "&" + sonroot['title'], lisitcontainer)

    elif isinstance(root, str):
        # lisitcontainer.append(str(rootstring.replace('\n', '')))  # 此处是去掉一步骤多结果情况直接拼接
        lisitcontainer.append(str(rootstring))  # 此处是一步骤多结果时,多结果合并

def get_case(root):
    rootstring = root['title']
    lisitcontainer = []
    traversal_xmind(root, rootstring, lisitcontainer)
    # for lisitcontaine in lisitcontainer:
    #     print(lisitcontaine)
    return lisitcontainer

def deal_excle(filename):
    '''
    tapd导入用例必须使用自有模板,因为此处复制模板Excel后生成新表
    :param filename: 模板地址
    :return:
    '''

    workbook = xlrd.open_workbook(filename)
    readbook = copy(workbook)
    idx = workbook.sheet_names()[0]
    # print('idx:', idx)
    # print('root001:',root)
    readbook.get_sheet(idx).name = str(root["title"])
    worksheet = readbook.get_sheet(0)

    return readbook, worksheet

def write_sheet(b, casename, casestep,expectresult):
    Worksheet.write(b, 0, casename)  # 用例名称
    Worksheet.write(b, 1, casestep)  # 用例步骤
    Worksheet.write(b, 2, expectresult)  # 预期结果

def deal_with_list(case_list):
    '''
    处理从xmind转换过来的用例list,并写入Excel中
    :param list: 传入从xmind转换好的用例列表
    :return:
    '''

    b = 1  # 记录写了多少行
    for row_case in case_list:
        case = row_case.split("&") # 用‘&’分隔,存在list中,这是一条用例
        caselength = len(case)
        if '前置条件' in row_case:
            casename = ''
            for i in range(0, caselength - 3):
                casename += case[i]  # 用例标题,(默认为从倒数第4个下标往前都是用例标题)
            casestep = case[-3] + ', 步骤:' + case[-2] # 用例步骤,(倒数第2个下标是步骤,倒数第3个下标识前置条件(必须有前置条件)
            expectresult = case[-1]  # 预期结果,(倒数第1个下标是预期结果)
            write_sheet(b, casename, casestep, expectresult)  # 写入Excel

        else:
            casename = ''
            for i in range(0, caselength - 2):
                casename += case[i]  # 用例标题,(无前置条件的情况下,默认为从倒数第3个下标往前都是用例标题)
            casestep = ' 步骤:' + case[-2] # 用例步骤,(倒数第2个下标是步骤)
            expectresult = case[-1]  # 预期结果,(倒数第1个下标是预期结果)
            write_sheet(b, casename, casestep, expectresult)  # 写入Excel

        b=b+1

if __name__ == '__main__':
    filePath = '报表需求用例.xmind'
    #file_name = 'case模板.xls'
    # 创建一个Workbook对象 编码encoding
    Workbook = xlwt.Workbook(encoding='utf-8', style_compression=0)
    # 添加一个sheet工作表、sheet名命名为Sheet1、cell_overwrite_ok=True允许覆盖写
    Worksheet = Workbook.add_sheet('Sheet1', cell_overwrite_ok=True)
    Worksheet.write(0, 0, '用例名称')
    Worksheet.write(0, 1, '操作步骤')
    Worksheet.write(0, 2, '预期结果')

    root = xmind_to_dict(filePath)[0]['topic'] # 解析成dict数据类型xmindparser.xmind_to_dict(filePath)
    print('root:',root)
    case_list = get_case(root)
    print('case:',case_list)
    deal_with_list(case_list)
    # Excel表保存的文件名字
    path='C:\\Users\\Dora_dora\\Desktop\\' + root["title"]+timestp + ".xls"
    Workbook.save(path)  # 此处可以填写生成位置
    #转换成csv格式文件
    # import pandas as pd
    # df = pd.read_excel('报表需求1650709981.xls')
    # df.to_csv('root["title"]+timestp.csv', index=False)

 

标签:case,title,python,sonroot,excel,xmind8,用例,xmind,root
来源: https://www.cnblogs.com/doradora/p/16183333.html