python 将xmind用例转换为excel用例
# -*- coding: utf-8 -*- from xmindparser import xmind_to_dict import xlwt # 记录列数,全局变量,还原方便 columnIndex = 0 # 记录行数 rowIndex = 1 # 每个完整用例子主题的个数 caseCount = 0 def get_xmind_zen_dict(ws, main_topic): """ ws:工作表 main_topic:主功能点 """ # 获取功能模块个数 feature_topic_count = len(case_topics_count) global rowIndex global columnIndex global caseCount title_list = ['用例目录', '用例名称', '前提条件', '操作步骤', '期望结果'] # 用例标题写入excel首行 for j in range(0, len(title_list)): ws.write(0, j, title_list[j]) j += 1 # 比对功能点,并写入固定列 for index in range(0, feature_topic_count): # 当前层级主题的标题 topic_title = case_topics_count[index]['title'] # 将已经提取出来的外层主题进行对比,设置为最外层的用例名 if topic_title in main_topic: columnIndex = 1 # 设置功能点key到固定列 # 将功能点写入固定列 ws.write(rowIndex, columnIndex, topic_title) if 'topics' in case_topics_count[index].keys(): # 开始读取功能用例,并记录用例个数 caseCount += 1 topic_topics = case_topics_count[index]['topics'] # 获取用例步骤 # 写前提条件 if 'topics' in topic_topics[0]: qianti_topics = topic_topics[0]['topics'] qianti_topics_j = [] for title in qianti_topics: qianti_topics = title['title'].encode('utf-8') # unicode编码转str qianti_topics_j.append(qianti_topics) # 追加内容 qianti = "\n".join([str(e) for e in qianti_topics_j]) # 拼接并换行 ws.write(rowIndex, columnIndex + 1, qianti) # 写步骤 step_topics = topic_topics[1]['topics'] step_topics_j = [] for title in step_topics: step_topics = title['title'].encode('utf-8') step_topics_j.append(step_topics) step = "\n".join([str(e) for e in step_topics_j]) # 拼接多个步骤并换行 ws.write(rowIndex, columnIndex + 2, step) # 写预期结果 yuqi_topics = topic_topics[2]['topics'] yuqi_topics_j = [] for title in yuqi_topics: yuqi_topics = title['title'].encode('utf-8') yuqi_topics_j.append(yuqi_topics) yuqi = "\n".join([str(e) for e in yuqi_topics_j]) ws.write(rowIndex, columnIndex + 3, yuqi) rowIndex += 1 # 写系统版本,功能模块 system_version = case_title + '-' + case_title_s for i in range(0, caseCount): ws.write(i+1, 0, system_version) print('用例总数%s:' % caseCount) if __name__ == '__main__': # 用例地址 file_path = 'Iot Platform V2.1系统管理优化用例.xmind' save_file = 'export.xlsx' # 首层画布 xmind_origin = xmind_to_dict(file_path) # 用例标题 case_title = xmind_origin[0]['topic']['title'] case_title_s = xmind_origin[0]['topic']['topics'][0]['title'] # 获取功能模块 # 主用例 case_topics = xmind_origin[0]['topic']['topics'] # 取出单个模块的功能用例 case_topics_count = case_topics[0]['topics'] # 需要把功能点title记录下来,以便进行匹配 main_topic = [] for topic in case_topics_count: main_topic.append(topic['title']) # 使用xlwt模块 wb = xlwt.Workbook(encoding='utf-8') ws = wb.add_sheet('Test', cell_overwrite_ok=True) # 用例集遍历 get_xmind_zen_dict(ws, main_topic) # 保存Excel文档 wb.save('export.xls')
本文来自博客园,作者:ReluStarry,转载请注明原文链接:https://www.cnblogs.com/relustarry/p/15526022.html