使用Python操作xmind文件
by:授客 QQ:1033553122
測試環境
Win10
Python 3.5.4
XMind-1.2.0.tar.gz
下載地址:
創建及更新xmind文件
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import xmind
from xmind.core.const import TOPIC_DETACHED
from xmind.core.markerref import MarkerId
from xmind.core.topic import TopicElement
# 加載已有xmind文件,如果不存在,則新建
workbook = xmind.load('D:\\example\\example.xmind')
first_sheet = workbook.getPrimarySheet() # 獲取第一個畫布
first_sheet.setTitle('First Sheet') # 設置畫布名稱
root_topic1 = first_sheet.getRootTopic() # 獲取畫布中心主題,默認創建畫布時會新建一個空白中心主題
root_topic1.setTitle('Example Topic') # 設置主題名稱
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")
# 除了新建子主題,還可以創建自由主題(注意:只有中心主題支持創建自由主題)
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")
second_sheet = workbook.createSheet() # 創建新畫布
second_sheet.setTitle('Second Sheet')
root_topic2 = second_sheet.getRootTopic()
root_topic2.setTitle('Root Node')
# 使用其它方式創建子主題元素
topic1 = TopicElement(ownerWorkbook=workbook)
topic1.setTopicHyperlink(first_sheet.getID()) # 為畫布創建一個來自第一個畫布的主題鏈接
topic1.setTitle("redirection to the first sheet")
topic2 = TopicElement(ownerWorkbook=workbook)
topic2.setTitle("topic with an url hyperlink")
topic2.setURLHyperlink("https://www.cnblogs.com/shouke") # 為子主題元素設置URL超鏈接
topic3 = TopicElement(ownerWorkbook=workbook)
topic3.setTitle("third node")
topic3.setPlainNotes("notes for this topic") # 為子主題設置備注 (F4 in XMind)
topic3.setTitle("topic with \n notes")
topic4 = TopicElement(ownerWorkbook=workbook)
topic4.setFileHyperlink("d:\\example\demo.jpg") # 為子主題元素設置文件超鏈接
topic4.setTitle("topic with a file")
topic1_1 = TopicElement(ownerWorkbook=workbook)
topic1_1.setTitle("sub topic")
topic1_1.addLabel("a label") # 為子主題添加標簽(official XMind only can a one label )
# 添加子主題到非中心主題
topic1.addSubTopic(topic1_1)
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)
# 為子主題添加子主題
topic1_1.addSubTopic(topic1_1_1)
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')
topic2.addSubTopic(topic2_1)
# 添加子主題元素到中心主題
root_topic2.addSubTopic(topic1)
root_topic2.addSubTopic(topic2)
root_topic2.addSubTopic(topic3)
root_topic2.addSubTopic(topic4)
# 遍歷子主題
topics = root_topic2.getSubTopics()
for index, topic in enumerate(topics):
topic.addMarker("priority-" + str(index + 1)) # 為主題添加標記(優先級圖標)
# 為子主題1和子主題2創建關系
second_sheet.createRelationship(topic1.getID(), topic2.getID(), "relationship test")
# xmind.save(workbook) # 保存並覆蓋原始文件
# 僅保存content.xml
# xmind.save(workbook=workbook, path="d:\\example\\other.xmind", only_content=True) # 不改動原始文件,另存為其它xmind文件
# 僅保存content.xml、comments.xml、styles.xml
# xmind.save(workbook=workbook, path="d:\\example\\other.xmind", except_revisions=True) # 不改動原始文件,另存為其它xmind文件
# 保存所有東西,Revisions除外,以節省空間(推薦)
# xmind.save(workbook=workbook, path="d:\\example\\other.xmind", except_revisions=True) # 不改動原始文件,另存為其它xmind文件
# 保存所有內容,並且另存為其它xmind文件(推薦)
xmind.save(workbook=workbook, path='d:\\example\\other.xmind') # 不改動原始文件,另存為其它xmind文件,等同 xmind.save(workbook, 'd:\\example\\exam.xmind')
運行結果
解析xmind文件
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json
import xmind
import pipes
def dict_to_prettify_json(data):
print(json.dumps(data, indent=4, separators=(',', ': ')))
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)
# 加載已有xmind文件,如果不存在,則新建
workbook = xmind.load('D:\\example\\example.xmind')
print(workbook.getData()) # 獲取整個xmind數據(字典的形式)
dict_to_prettify_json(workbook.getData())
# 獲取某個畫布的數據(字典的形式)
first_sheet = workbook.getPrimarySheet()
dict_to_prettify_json(first_sheet.getData())
# 獲取某個主題數據(字典的形式)
root_topic = first_sheet.getRootTopic()
dict_to_prettify_json(root_topic.getData())
# 獲取評論數據
commentsbook = workbook.commentsbook
print(commentsbook.getData())
# 自定義解析
custom_parse_xmind(workbook)