使用python把xmind文件转换为xlsx文件测试用例

用例的格式可以自己修改

import xlwt,xlrd
from xmindparser import xmind_to_dict
from openpyxl.styles import Font
from openpyxl import load_workbook


def resolvePath(dict,lists,title):
# title去除首尾空格
title = title.strip()
# 如果title是空字符串,则直接获取value
if len(title) == 0:
concatTitle = dict['title'].strip()
# print(concatTitle)
else:
concatTitle = title + '\t' + dict['title'].strip()

if dict.__contains__('topics')==False:
lists.append(concatTitle)
else:
for d in dict['topics']:
resolvePath(d,lists,concatTitle)


def xmind_cat(list,excelname):
# print(f'list是{list}')
# print(list)

f = xlwt.Workbook()
# 生成excel文件,单sheet,sheet名为:out1["title"]
out1 = xmind_to_dict(filename)[0]["topic"]
sheet = f.add_sheet(out1["title"] , cell_overwrite_ok=True)


row0 = [ '用例标题','步骤','预期', '用例类型','使用阶段']
# 生成第一行中固定表头内容
for i in range(0, len(row0)):
sheet.write(0, i, row0[i])

# 增量索引
index = 0

for h in range(0,len(list)):
lists = []
resolvePath(list[h], lists, '')
for j in range(0, len(lists)):
# print(lists)

lists[j] = lists[j].split('\t')

for n in range(0, len(lists[j])):
# print(lists[j][n])
out1 = xmind_to_dict(filename)[0]["topic"]
# 获取xmind的中心主题,拼接用例
ii = out1["title"]+"-".join(lists[j])
# print(lists[j])
# 获取预期结果
jj = lists[j][-1]
# 写入表中
sheet.write(j+index+1,0,ii)
sheet.write(j+index+1,1,ii)
sheet.write(j+index+1,2,jj)
sheet.write(j+index+1,3,'功能测试')
sheet.write(j+index+1,4,'系统测试阶段')


# 遍历结束lists,给增量索引赋值,跳出for j循环,开始for h循环
if j == len(lists) - 1:
index += len(lists)

f.save(excelname)

def maintest( filename):
out = xmind_to_dict(filename)
excelname = filename.split('/')[-1].split('.')[0] + '.xlsx'
xmind_cat(out[0]['topic']['topics'],excelname)
# 获取文档的用例数
filepath= excelname
wb= xlrd.open_workbook(filepath)
out1 = xmind_to_dict(filename)[0]["topic"]
sheet1 =wb.sheet_by_name(out1["title"])
n = sheet1.nrows-1
print("用例数为:",n)


if __name__ == '__main__':
filename = 'xmind文件路径'
maintest(filename)
posted @ 2021-08-30 21:35  竹贤  阅读(255)  评论(0编辑  收藏  举报