python之x-mind中的测试用例内容转excel

发布时间 2023-07-05 16:53:48作者: 阿华阿

注意:

导包xlwt生成的excel的文件后缀只能是xls,用xlsx打开文件会报错

一、文件格式

1.x-mind用例格式,若觉得字段太多可在代码中缩减

 2.转后的excel格式

 二、脚本(原贴:caituotuo.top/c2d10f21.html

修改最下面文件路径即可用

import xlwt
from xmindparser import xmind_to_dict
def resolve_path(dict_, lists, title):
"""
通过递归取出每个主分支下的所有小分支并将其作为一个列表
:param dict_:
:param lists:
:param title:
:return:
"""
# 去除title首尾空格
title = title.strip()
# 若title为空,则直接取value
if len(title) == 0:
concat_title = dict_["title"].strip()
else:
concat_title = title + "\t" + dict_["title"].strip()
if not dict_.__contains__("topics"):
lists.append(concat_title)
else:
for d in dict_["topics"]:
resolve_path(d, lists, concat_title)


def xmind_to_excel(list_, excel_path):
f = xlwt.Workbook()
# 生成单sheet的Excel文件,sheet名自取
sheet = f.add_sheet("测试用例", cell_overwrite_ok=True)
#第一行表头
frist_row_header=['测试用例']
sheet.write_merge(0,0,0,11,frist_row_header[0])
#第二行表头
two_row_header=['文件密级: ○高密 ○机密 ○秘密 ○内部公开 ○外部公开 保密期:○无 ○______年']
sheet.write_merge(1, 1, 0, 11, two_row_header[0])
#第三行表头
three_row_header=['项目编号','项目名称','测试日期','测试类型','测试版本']
sheet.write_merge(2, 2, 0, 1, three_row_header[0])
sheet.write(2,3, three_row_header[1])
sheet.write(2,6, three_row_header[2])
sheet.write(2,8, three_row_header[3])
sheet.write(2,10, three_row_header[4])
# 第四行表头
row_header = ["序号", "模块名称", "子功能","用例描述","前置条件","测试数据","操作步骤","测试人员","期望结果","首次测试结果","复测结果","测试问题及其原因","备注"]
for i in range(0, len(row_header)):
sheet.write(3, i, row_header[i])

# 增量索引
index = 0
all_row=0
for h in range(0, len(list_)):
lists:List[Any] = []
resolve_path(list_[h], lists, "")
all_row=len(lists)+all_row
print(lists)
# print('\n'.join(lists)) # 主分支下的小分支

for j in range(0, len(lists)):
# 将主分支下的小分支构成列表
lists[j] = lists[j].split('\t')
# print(lists[j])

for n in range(0, len(lists[j])):
# 生成第一列的序号
sheet.write(j + index + 4, 0, j + index + 1)
sheet.write(j + index + 4, n + 1, lists[j][n])
# 自定义内容,比如:测试点/用例标题、预期结果、实际结果、操作步骤、优先级……
# 这里为了更加灵活,除序号、模块、功能点的标题固定,其余以【自定义+序号】命名,如:自定义1,需生成Excel表格后手动修改
# if n >= 2:
# sheet.write(0, n + 1, "自定义" + str(n - 1))
# 遍历完lists并给增量索引赋值,跳出for j循环,开始for h循环
if j == len(lists) - 1:
index += len(lists)
#倒数第三行
row_lenth=all_row+6
sheet.write_merge(row_lenth,row_lenth,0,2,"最终结论")
sheet.write_merge(row_lenth, row_lenth,3 ,11, "□通过 □不通过")
#倒数第二行
row_lenth = all_row +7
context= '备注:①NA表示不适用,当结果为NA和NG时,需将问题及原因进行简单描述;' \
'②当首次测试经过为NG时,需要在开发修改完成后进行复测,若出现多次复测情况,需要在备注中说明次数。'
sheet.write_merge(row_lenth, row_lenth, 0, 11, context)
#倒数第一行
row_lenth = all_row + 8
sheet.write(row_lenth,0,"制定:")
sheet.write(row_lenth, 5, "审核:")
sheet.write(row_lenth, 9, "批准:")
f.save(excel_path)

if __name__ == '__main__':
xmind_path_ = r"D:\\pythonProject_01\\qqq\\sample\\Test.xmind"
run(xmind_path_)