一方庭、實現(xiàn)方式
1、使用xmind專業(yè)版本自帶功能:文件 -> 導出為Excel...
但若是破解版或者試用版本税灌,則會提示需訂閱:
2吹害、使用百度腦圖導入xmind文件螟凭,再導出為txt文件,最后復制到excel文檔中
3它呀、使用Python腳本轉換
二螺男、Python使用模塊介紹
xmindparser
1、安裝
pip install xmindparser
2纵穿、GitHub倉庫地址:
https://github.com/tobyqin/xmindparser
3下隧、使用命令行轉換成json或xml文件
cd /your/xmind/dir
xmindparser your.xmind -json
xmindparser your.xmind -xml
4、通過代碼轉換成字典
from xmindparser import xmind_to_dict
d = xmind_to_dict('/path/to/your/xmind_file')
print(d)
openpyxl
1谓媒、安裝
pip install openpyxl
2淆院、文檔地址
https://openpyxl.readthedocs.io/en/stable/index.html
3、創(chuàng)建excel文檔句惯,并保存
# 創(chuàng)建一個Excel文件
wb = Workbook()
ws = wb.active
# 保存Excel文檔
wb.save('test.xlsx')
4土辩、插入數(shù)據(jù)
ws.cell(row=rowIndex, column=columnIndex).value = value
三支救、Python代碼具體實現(xiàn)
1、xmind與excel轉換的基本思想
2拷淘、使用xmindparser完整代碼
# -*- coding: utf-8 -*-
import os
import json
import data.excelTool
from openpyxl import Workbook
from xmindparser import xmind_to_dict
#記錄列數(shù)各墨,全局變量,還原方便
columnIndex = 1
#記錄行數(shù)
rowIndex = 1
#每個完整用例子主題的個數(shù)
caseCount = 0
#同層級主題個數(shù)
level_equal = 0
def get_xmind_zen_dict(ws, case_topics,main_topic):
"""
:param case_topics: 用例集
:return:
遍歷過程中需要有以下標記
1辕棚、層級標記:主標題 - 分支標題 - 同級標題 - 子標題
2欲主、用例條數(shù):對應Excel的行數(shù)
"""
# 首層
feature_topic_count = len(case_topics)
global rowIndex
global columnIndex
global caseCount
global level_equal
# 遍歷用例集,直到無topics表示用例遍歷完成
for index in range(0, feature_topic_count):
# 當前層級主題的標題
topic_title = case_topics[index]['title']
print(topic_title)
#將已經(jīng)提取出來的外層主題進行對比逝嚎,設置為最外層的用例名
if topic_title in main_topic:
columnIndex = 1
# 首先扁瓢,將主功能占位
data.excelTool.setCellValue(ws=ws, columnIndex=columnIndex, rowIndex=rowIndex,
value=topic_title)
columnIndex += 1
if 'topics' in case_topics[index].keys():
# 當前層級下子主題數(shù)組
# 每提取一次子主題,count即++一次补君,記錄分支數(shù)量
caseCount += 1
topic_topics = case_topics[index]['topics']
# 遞歸
get_xmind_zen_dict(ws, topic_topics,main_topic)
else:
# 某一個分支遍歷結束引几,需要做一次標記,行列需要還原
#同層級最末分支
level_equal_temp = len(case_topics)
#處理行挽铁、列數(shù)值
if level_equal_temp > 1:
columnIndex -= 1
level_equal += 1
rowIndex += 1
#一次for循環(huán)結束后伟桅,還原
if level_equal == feature_topic_count:
columnIndex = columnIndex - caseCount +1
if __name__ == '__main__':
# 用例地址
file_path = 'Xmind轉成Excel.xmind'
# 首層畫布
xmind_origin = xmind_to_dict(file_path)
# 用例標題
case_title = xmind_origin[0]['topic']['title']
# 主用例
case_topics = xmind_origin[0]['topic']['topics']
#需要把最外層的主題內容記錄下來叽掘,以便進行匹配
main_topic = []
for topic in case_topics:
main_topic.append(topic['title'])
# 創(chuàng)建一個Excel文件
wb = Workbook()
ws = wb.active
# 用例集遍歷
get_xmind_zen_dict(ws, case_topics,main_topic)
# 保存Excel文檔
wb.save('test.xlsx')