用python实现xmind用例转换为excel/csv用例

from xmindparser import xmind_to_dict
from openpyxl import Workbook

# 解析XMind文件
xmind_file = 'path/to/xmind/file.xmind'
xmind_data = xmind_to_dict(xmind_file)

# 创建Excel文件
excel_file = 'path/to/excel/file.xlsx'
wb = Workbook()
ws = wb.active

# 定义用例表格的列名
ws['A1'] = 'Title'
ws['B1'] = 'Description'
ws['C1'] = 'Priority'
ws['D1'] = 'Steps'
ws['E1'] = 'Expected Result'

# 获取用例数据
case_data = xmind_data['topics'][0]  # 假设用例数据在第一个主题下

# 将用例数据写入Excel文件
row = 2  # 从第二行开始写入数据

def process_topic(topic):
    global row
    title = topic['title']
    description = topic['topics'][0]['title']
    priority = topic['topics'][0]['topics'][0]['title']
    steps = topic['topics'][0]['topics'][0]['topics']
    for step in steps:
        step_title = step['title']
        step_description = step['topics'][0]['title']
        # 将用例数据写入Excel文件的相应单元格
        ws[f'A{row}'] = title
        ws[f'B{row}'] = description
        ws[f'C{row}'] = priority
        ws[f'D{row}'] = step_title
        ws[f'E{row}'] = step_description
        row += 1

process_topic(case_data)

# 保存Excel文件
wb.save(excel_file)

xmind转换成csv

import csv
from xmindparser import xmind_to_dict

# 解析XMind文件
xmind_file = 'path/to/xmind/file.xmind'
xmind_data = xmind_to_dict(xmind_file)

# 创建CSV文件
csv_file = 'path/to/csv/file.csv'
csv_columns = ['Title', 'Description', 'Priority', 'Steps', 'Expected Result']
csv_data = []

# 获取用例数据
case_data = xmind_data['topics'][0]  # 假设用例数据在第一个主题下

def process_topic(topic):
    title = topic['title']
    description = topic['topics'][0]['title']
    priority = topic['topics'][0]['topics'][0]['title']
    steps = topic['topics'][0]['topics'][0]['topics']
    for step in steps:
        step_title = step['title']
        step_description = step['topics'][0]['title']
        # 将用例数据添加到CSV数据列表中
        csv_data.append({
            'Title': title,
            'Description': description,
            'Priority': priority,
            'Steps': step_title,
            'Expected Result': step_description
        })

process_topic(case_data)

# 将CSV数据写入CSV文件
with open(csv_file, 'w', newline='') as file:
    writer = csv.DictWriter(file, fieldnames=csv_columns)
    writer.writeheader()
    for data in csv_data:
        writer.writerow(data)

你可能感兴趣的:(Python,python)