XMind 是基於 Python 實現,提供了對 XMind思惟導圖進行建立、解析、更新的一站式解決方案!node
pip3 install XMind
or
pip3 install xmind
複製代碼
pip3 install -U XMind
複製代碼
def gen_my_xmind_file():
# 一、若是指定的XMind文件存在,則加載,不然建立一個新的
workbook = xmind.load("my.xmind")
# 二、獲取第一個畫布(Sheet),默認新建一個XMind文件時,自動建立一個空白的畫布
sheet1 = workbook.getPrimarySheet()
# 對第一個畫布進行設計完善,具體參照下一個函數
design_sheet1(sheet1)
# 三、建立第二個畫布
gen_sheet2(workbook, sheet1)
# 四、保存(若是指定path參數,另存爲該文件名)
xmind.save(workbook, path='test.xmind')
複製代碼
def design_sheet1(sheet1):
# ***** 第一個畫布 *****
sheet1.setTitle("first sheet") # 設置畫布名稱
# 獲取畫布的中心主題,默認建立畫布時會新建一個空白中心主題
root_topic1 = sheet1.getRootTopic()
root_topic1.setTitle("root node") # 設置主題名稱
# 建立一個子主題,並設置其名稱
sub_topic1 = root_topic1.addSubTopic()
sub_topic1.setTitle("first sub topic")
sub_topic2 = root_topic1.addSubTopic()
sub_topic2.setTitle("second sub topic")
sub_topic3 = root_topic1.addSubTopic()
sub_topic3.setTitle("third sub topic")
sub_topic4 = root_topic1.addSubTopic()
sub_topic4.setTitle("fourth sub topic")
# 除了新建子主題,還能夠建立自由主題(注意:只有中心主題支持建立自由主題)
detached_topic1 = root_topic1.addSubTopic(topics_type=TOPIC_DETACHED)
detached_topic1.setTitle("detached topic")
detached_topic1.setPosition(0, 30)
# 建立一個子主題的子主題
sub_topic1_1 = sub_topic1.addSubTopic()
sub_topic1_1.setTitle("I'm a sub topic too")
複製代碼
def gen_sheet2(workbook, sheet1):
# ***** 設計第二個畫布 *****
sheet2 = workbook.createSheet()
sheet2.setTitle("second sheet")
# 獲取畫布的中心主題
root_topic2 = sheet2.getRootTopic()
root_topic2.setTitle("root node")
# 使用另一種方法建立子主題
topic1 = TopicElement(ownerWorkbook=workbook)
# 給子主題添加一個主題間超連接,經過指定目標主題ID便可,這裏連接到第一個畫布
topic1.setTopicHyperlink(sheet1.getID())
topic1.setTitle("redirection to the first sheet")
topic2 = TopicElement(ownerWorkbook=workbook)
topic2.setTitle("topic with an url hyperlink")
# 給子主題添加一個URL超連接
topic2.setURLHyperlink("https://github.com/zhuifengshen/xmind")
topic3 = TopicElement(ownerWorkbook=workbook)
topic3.setTitle("third node")
# 給子主題添加一個備註(快捷鍵F4)
topic3.setPlainNotes("notes for this topic")
topic3.setTitle("topic with \n notes")
topic4 = TopicElement(ownerWorkbook=workbook)
# 給子主題添加一個文件超連接
topic4.setFileHyperlink("logo.png")
topic4.setTitle("topic with a file")
topic1_1 = TopicElement(ownerWorkbook=workbook)
topic1_1.setTitle("sub topic")
# 給子主題添加一個標籤(目前XMind軟件僅支持添加一個,快捷鍵)
topic1_1.addLabel("a label")
topic1_1_1 = TopicElement(ownerWorkbook=workbook)
topic1_1_1.setTitle("topic can add multiple markers")
# 給子主題添加兩個圖標
topic1_1_1.addMarker(MarkerId.starBlue)
topic1_1_1.addMarker(MarkerId.flagGreen)
topic2_1 = TopicElement(ownerWorkbook=workbook)
topic2_1.setTitle("topic can add multiple comments")
# 給子主題添加一個批註(評論)
topic2_1.addComment("I'm a comment!")
topic2_1.addComment(content="Hello comment!", author='devin')
# 將建立好的子主題添加到其父主題下
root_topic2.addSubTopic(topic1)
root_topic2.addSubTopic(topic2)
root_topic2.addSubTopic(topic3)
root_topic2.addSubTopic(topic4)
topic1.addSubTopic(topic1_1)
topic2.addSubTopic(topic2_1)
topic1_1.addSubTopic(topic1_1_1)
# 給中心主題下的每一個子主題添加一個優先級圖標
topics = root_topic2.getSubTopics()
for index, topic in enumerate(topics):
topic.addMarker("priority-" + str(index + 1))
# 添加一個主題與主題之間的聯繫
sheet2.createRelationship(topic1.getID(), topic2.getID(), "relationship test")
複製代碼
具體代碼參考:create_xmind.pypython
import xmind
workbook = xmind.load('demo.xmind')
print(workbook.getData())
print(workbook.to_prettify_json())
Output:
[ # 畫布列表
{ # 第1個畫布數據
"id": "2cc3b068922063a81a20029655", # 畫布ID
"title": "first sheet", # 畫布名稱
"topic": { # 中心主題
"id": "2cc3b06892206f95288e487b6c", # 主題ID
"link": null, # 超連接信息
"title": "root node", # 主題名稱
"note": null, # 備註信息
"label": null, # 便籤信息
"comment": null, # 批註(評論)信息
"markers": [], # 圖標列表
"topics": [ # 子主題列表
{
"id": "2cc3b06892206c816e1cb55ddc", # 子主題ID
"link": null,
"title": "first sub topic",
"note": null,
"label": null,
"comment": null,
"markers": [],
"topics": [ # 子主題下的子主題列表
{
"id": "b0ed74214dbca939935b981906",
"link": null,
"title": "I'm a sub topic too",
"note": null,
"label": null,
"comment": null,
"markers": []
}
]
},
{
"id": "b0ed74214dbca693b947ef03fa",
"link": null,
"title": "second sub topic",
"note": null,
"label": null,
"comment": null,
"markers": []
},
{
"id": "b0ed74214dbca1fe9ade911b94",
"link": null,
"title": "third sub topic",
"note": null,
"label": null,
"comment": null,
"markers": []
},
{
"id": "b0ed74214dbcac00c0eb368b53",
"link": null,
"title": "fourth sub topic",
"note": null,
"label": null,
"comment": null,
"markers": []
}
]
}
},
{
"id": "b0ed74214dbcafdd0799f81ebf",
"title": "second sheet", # 第2個畫布數據
"topic": {
"id": "b0ed74214dbcac7567f88365c2",
"link": null,
"title": "root node",
"note": null,
"label": null,
"comment": null,
"markers": [],
"topics": [
{
"id": "b0ed74214dbca8bfdc2b60df47",
"link": "xmind:#2cc3b068922063a81a20029655",
"title": "redirection to the first sheet",
"note": null,
"label": null,
"comment": null,
"markers": [
"priority-1"
],
"topics": [
{
"id": "e613d79938591579e707a7a161",
"link": null,
"title": "sub topic",
"note": null,
"label": "a label",
"comment": null,
"markers": [],
"topics": [
{
"id": "e613d799385912cca5eb579fb3",
"link": null,
"title": "topic can add multiple markers",
"note": null,
"label": null,
"comment": null,
"markers": [
"star-blue",
"flag-green"
]
}
]
}
]
},
{
"id": "e613d79938591ef98b64a768db",
"link": "https://xmind.net",
"title": "topic with an url hyperlink",
"note": null,
"label": null,
"comment": null,
"markers": [
"priority-2"
],
"topics": [
{
"id": "e613d799385916ed8f3ea382ca",
"link": null,
"title": "topic can add multiple comments",
"note": null,
"label": null,
"comment": "I'm a comment!\nHello comment!",
"markers": []
}
]
},
{
"id": "e613d799385919451116404d66",
"link": null,
"title": "topic with \n notes",
"note": "notes for this topic",
"label": null,
"comment": null,
"markers": [
"priority-3"
]
},
{
"id": "e613d7993859156671fa2c12a5",
"link": "file:///Users/zhangchuzhao/Project/python/tmp/xmind/example/xminddemo/logo.png",
"title": "topic with a file",
"note": null,
"label": null,
"comment": null,
"markers": [
"priority-4"
]
}
]
}
}
]
複製代碼
import xmind
workbook = xmind.load('demo.xmind')
sheet = workbook.getPrimarySheet()
print(sheet.getData())
Output:
{
"id": "2cc3b068922063a81a20029655",
"title": "first sheet",
"topic": {
"id": "2cc3b06892206f95288e487b6c",
"link": null,
"title": "root node",
"note": null,
"label": null,
"comment": null,
"markers": [],
"topics": [
{
"id": "2cc3b06892206c816e1cb55ddc",
"link": null,
"title": "first sub topic",
"note": null,
"label": null,
"comment": null,
"markers": [],
"topics": [
{
"id": "b0ed74214dbca939935b981906",
"link": null,
"title": "I'm a sub topic too",
"note": null,
"label": null,
"comment": null,
"markers": []
}
]
},
{
"id": "b0ed74214dbca693b947ef03fa",
"link": null,
"title": "second sub topic",
"note": null,
"label": null,
"comment": null,
"markers": []
},
{
"id": "b0ed74214dbca1fe9ade911b94",
"link": null,
"title": "third sub topic",
"note": null,
"label": null,
"comment": null,
"markers": []
},
{
"id": "b0ed74214dbcac00c0eb368b53",
"link": null,
"title": "fourth sub topic",
"note": null,
"label": null,
"comment": null,
"markers": []
}
]
}
}
複製代碼
import xmind
workbook = xmind.load('demo.xmind')
sheet = workbook.getPrimarySheet()
root_topic = sheet.getRootTopic()
print(root_topic.getData())
Output:
{
"id": "2cc3b06892206f95288e487b6c",
"link": null,
"title": "root node",
"note": null,
"label": null,
"comment": null,
"markers": [],
"topics": [
{
"id": "2cc3b06892206c816e1cb55ddc",
"link": null,
"title": "first sub topic",
"note": null,
"label": null,
"comment": null,
"markers": [],
"topics": [
{
"id": "b0ed74214dbca939935b981906",
"link": null,
"title": "I'm a sub topic too",
"note": null,
"label": null,
"comment": null,
"markers": []
}
]
},
{
"id": "b0ed74214dbca693b947ef03fa",
"link": null,
"title": "second sub topic",
"note": null,
"label": null,
"comment": null,
"markers": []
},
{
"id": "b0ed74214dbca1fe9ade911b94",
"link": null,
"title": "third sub topic",
"note": null,
"label": null,
"comment": null,
"markers": []
},
{
"id": "b0ed74214dbcac00c0eb368b53",
"link": null,
"title": "fourth sub topic",
"note": null,
"label": null,
"comment": null,
"markers": []
}
]
}
複製代碼
import xmind
workbook = xmind.load('demo.xmind')
custom_parse_xmind(workbook)
def custom_parse_xmind(workbook):
elements = {}
def _echo(tag, element, indent=0):
title = element.getTitle()
elements[element.getID()] = title
print('\t' * indent, tag, ':', pipes.quote(title))
def dump_sheet(sheet):
root_topic = sheet.getRootTopic()
_echo('RootTopic', root_topic, 1)
for topic in root_topic.getSubTopics() or []:
_echo('AttachedSubTopic', topic, 2)
for topic in root_topic.getSubTopics(xmind.core.const.TOPIC_DETACHED) or []:
_echo('DetachedSubtopic', topic, 2)
for rel in sheet.getRelationships():
id1, id2 = rel.getEnd1ID(), rel.getEnd2ID()
print('Relationship: [%s] --> [%s]' % (elements.get(id1), elements.get(id2)))
for sheet in workbook.getSheets():
_echo('Sheet', sheet)
dump_sheet(sheet)
Output:
Sheet : 'first sheet'
RootTopic : 'root node'
AttachedSubTopic : 'first sub topic'
AttachedSubTopic : 'second sub topic'
AttachedSubTopic : 'third sub topic'
AttachedSubTopic : 'fourth sub topic'
DetachedSubtopic : 'detached topic'
Sheet : 'second sheet'
RootTopic : 'root node'
AttachedSubTopic : 'redirection to the first sheet'
AttachedSubTopic : 'topic with an url hyperlink'
AttachedSubTopic : 'topic with notes'
AttachedSubTopic : 'topic with a file'
Relationship: [redirection to the first sheet] --> [topic with an url hyperlink]
複製代碼
具體代碼參考:parse_xmind.pygit
import xmind
# 加載XMind文件demo.xmind
workbook = xmind.load('demo.xmind')
primary_sheet = workbook.getPrimarySheet()
root_topic = primary_sheet.getRootTopic()
# 給中心主題添加一個星星圖標
root_topic.addMarker(MarkerId.starRed)
# 第1種:默認保存全部的內容,這裏保存時另存爲xmind_update_demo.xmind(推薦)
xmind.save(workbook=workbook, path='xmind_update_demo.xmind')
# 第2種:只保存思惟導圖內容content.xml核心文件,適用於沒有添加評論、自定義樣式和附件的狀況
xmind.save(workbook=workbook, path='xmind_update_demo1.xmind', only_content=True)
# 第3種:只保存content.xml、comments.xml、styles.xml三個核心文件,適用於沒有附件的狀況
xmind.save(workbook=workbook, path='xmind_update_demo2.xmind', except_attachments=True)
# 四、除了修改記錄,其餘內容都保存,由於XMind文件的修改記錄文件夾比較大,以便節約內存(推薦)
xmind.save(workbook=workbook, path='xmind_update_demo3.xmind', except_revisions=True)
# 五、不指定保存路徑,直接更新原文件
xmind.save(workbook)
複製代碼
具體代碼參考:update_xmind.pygithub
其中,暫不支持的元素(平常也比較少用到)web
XMind2TestCase:一個高效測試用例設計的解決方案!json
該方案經過制定測試用例通用模板, 而後使用 XMind 這款廣爲流傳且開源的思惟導圖工具進行用例設計。bash
而後基於通用的測試用例模板,在 XMind 文件上解析並提取出測試用例所需的基本信息, 合成常見測試用例管理系統所需的用例導入文件。app
實現將 XMind 設計測試用例的便利與常見測試用例系統的高效管理完美結合起來了,提高平常測試工做的效率!函數
使用流程以下:工具
python3 -m unittest discover
複製代碼
每次在 __ about __.py 更新版本號後,運行如下命令,實現自動化更新打包上傳至 PYPI ,同時根據其版本號自動打 Tag 並推送到倉庫:
python3 setup.py pypi
複製代碼
在此,衷心感謝 XMind 思惟導圖官方創造了這麼一款激發靈感、創意,提高工做、生活效率的高價值生產力產品, 同時還開源 xmind-sdk-python 工具幫助開發者構建本身的 XMind 文件 ,本項目正是基於此工具進行擴展和升級,受益不淺,感恩!
得益於開源,也將堅持開源,併爲開源貢獻本身的點滴之力。後續,將繼續根據實際項目須要,按期進行維護更新和完善,歡迎大夥的使用和意見反饋,謝謝!
(若是本項目對你有幫助的話,也歡迎 star )