my_xmind

这段代码实现了一个从Xmind文件转换为Excel的工作,它读取Xmind文件中的测试用例信息,包括标题、描述、优先级、步骤等,并将这些信息写入Excel表格中,最后保存为指定的文件名。
import xlwt  # 导入模块
from xmindparser import xmind_to_dict

xm = xmind_to_dict("xmind_to_excel_jira.xmind")[0]['topic']  # 读取xmind数据
workbook = xlwt.Workbook(encoding='utf-8')  # 创建workbook对象
worksheet = workbook.add_sheet(xm["title"], cell_overwrite_ok=True)  # 创建工作表,并设置可以重写单元格内容
row0 = ['标题', '描述', '优先级', '步骤ID', '步骤', '预期结果', '经办人', '需求', '测试用例集']  # 写成excel表格用例的要素

for i in range(len(row0)):
    worksheet.write(0, i, row0[i])

x = 0  # 写入数据的当前行数
for i in range(len(xm["topics"])):
    test_module = xm["topics"][i]
    for j in range(len(test_module["topics"])):
        case_name = test_module["topics"][j]
        for k in range(len(case_name["topics"])):
            test_description = case_name["topics"][k]
            for m in range(len(test_description["topics"])):
                test_priority = test_description["topics"][m]
                c1 = len(test_priority["topics"])  # 执行步骤有几个
                for n in range(len(test_priority["topics"])):
                    x += 1
                    test_step = test_priority["topics"][n]
                    test_except = test_step["topics"][0]
                    worksheet.write(x, 0, case_name["title"])
                    worksheet.write(x, 1, test_description["title"])
                    worksheet.write(x, 2, test_priority["title"])
                    worksheet.write(x, 3, f"{n + 1}")
                    worksheet.write(x, 4, f"{n + 1}." + test_step["title"])  # 执行步骤
                    worksheet.write(x, 5, f"{n + 1}." + test_except["title"])  # 预期结果

workbook.save(xm["title"] + ".xls")  # xls名称取xmind主题名称
from xmindparser import xmind_to_dict
import xmind
import sys
import xlwt
import argparse
import os
import time

row_num = 1
# 设置用例标题所在列
name_col_num = 0
# 设置用例描述所在列
description_col_num = 1
# 设置用例优先级所在列
priority_col_num = 2
# 设置用例步骤ID所在列
stepsId_col_num = 3
# 设置用例步骤所在列
steps_col_num = 4
# 设置预期结果所在列
expect_col_num = 5
# 设置经办人所在列
operator_col_num = 6
# 需求编码
pb_col_num = 7
# 测试用例集
testCases_col_num = 8


# 用于设置插入excel的标题
def set_excel_header():
    n = 0
    header = ['标题', '描述', '优先级', '步骤ID', '步骤', '预期结果', '经办人', '需求', '测试用例集']
    for i in header:
        ws.write(0, n, i)
        n += 1


# 该方法用于插入excel
def insert_excel(text, row, col):
    ws.write(text, row, col)


# 该方法用于生成用例名称
def write_testCase():
    xm = get_xmind_data()
    x = 0  # 写入数据的当前行数
    z = 0  # 用例的编号
    for i in range(len(xm["topics"])):
        test_module = xm["topics"][i]
        for j in range(len(test_module["topics"])):
            test_suit = test_module["topics"][j]
            for k in range(len(test_suit["topics"])):
                test_case = test_suit["topics"][k]
                z += 1
                c1 = len(test_case["topics"])  # 执行步骤有几个
                for n in range(len(test_case["topics"])):
                    x += 1
                    test_step = test_case["topics"][n]
                    test_except = test_step["topics"][0]
                    worksheet.write(x, 4, f"{n + 1}." + test_except["title"])  # 预期结果
                    worksheet.write(x, 3, f"{n + 1}." + test_step["title"])  # 执行步骤
                worksheet.write_merge(x - c1 + 1, x, 0, 0, z)  # testcaseid
                worksheet.write_merge(x - c1 + 1, x, 1, 1, test_module["title"])  # 测试需求名称
                worksheet.write_merge(x - c1 + 1, x, 2, 2, test_case["title"])  # 测试用例名称
    workbook.save(xm["title"] + ".xls")  # xls名称取xmind主题名称

# 重新设置生成文件名
def set_file_name(input_filename, output_filename):
    if output_filename == '测试案例.xls':
        str = input_filename.replace('.xmind', '_') + output_filename
        path2 = os.getcwd() + '\\' + str
        if os.path.exists(path2):
            time1 = time.strftime("%Y%m%d%H%M%S", time.localtime())
            str = input_filename.replace('.xmind', '_') + '测试案例' + time1 + '.xls'
        return str
    else:
        path3 = os.getcwd() + '\\' + output_filename
        if os.path.exists(path3):
            print('检测到当前路径下已有存在文件:', output_filename, '\n是否继续覆盖并生成文件?Y:继续')
            isg = input()
            if isg == 'Y' or isg == 'y':
                os.remove(path3)
            else:
                print(output_filename, '文件生成操作已取消。')
                sys.exit()
        return output_filename


def get_xmind_data():
    workbook = xmind.load(args.inputfile)
    xm = xmind_to_dict(workbook)[0]['topic']
    return xm


parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', type=str, dest='inputfile', default='思维导图.xmind',
                    help='Default inputfile is 思维导图.xmind')
parser.add_argument('-o', '--output-file', type=str, dest='outputfile', default='测试案例.xls',
                    help='Default outputfile is 测试案例.xls')
args = parser.parse_args()

if args.inputfile is None:
    parser.print_help()
    exit()

path3 = os.getcwd() + '\\' + args.inputfile
if not os.path.exists(path3):
    print('\n文件\"' + path3 + '\"不存在,请确认后再次操作,谢谢!')
    exit()

file_name = set_file_name(args.inputfile, args.outputfile)

print('开始将', args.inputfile, '文件转换为excel。。。')



sheet = xlwt.Workbook()
ws = sheet.add_sheet('Jira导入模板', cell_overwrite_ok=True)
set_excel_header()


sheet.save(file_name)
print('导出完成,已生成文件:', file_name, ',总计', row_num - 1, '条用例。')
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值