天天看點

python實作xmind_Python 使用Python操作xmind檔案

Python

Python開發

Python語言

Python 使用Python操作xmind檔案

python實作xmind_Python 使用Python操作xmind檔案

使用Python操作xmind檔案

by:授客QQ:1033553122

測試環境

Win10

Python 3.5.4

XMind-1.2.0.tar.gz

下載下傳位址:

建立及更新xmind檔案

#!/usr/bin/env python

# -*- coding:utf-8 -*-

importxmind

fromxmind.core.const importTOPIC_DETACHED

fromxmind.core.markerref importMarkerId

fromxmind.core.topic importTopicElement

#加載已有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 withnnotes")

topic4 = TopicElement(ownerWorkbook=workbook)

topic4.setFileHyperlink("d:\exampledemo.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()

forindex, topic inenumerate(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')

運作結果

python實作xmind_Python 使用Python操作xmind檔案
python實作xmind_Python 使用Python操作xmind檔案

解析xmind檔案

#!/usr/bin/env python

# -*- coding:utf-8 -*-

importjson

importxmind

importpipes

defdict_to_prettify_json(data):

print(json.dumps(data, indent=4, separators=(',', ': ')))

defcustom_parse_xmind(workbook):

elements = {}

def_echo(tag, element, indent=0):

title = element.getTitle()

elements[element.getID()] = title

print('t'* indent, tag, ':', pipes.quote(title))

defdump_sheet(sheet):

root_topic = sheet.getRootTopic()

_echo('RootTopic', root_topic, 1)

fortopic inroot_topic.getSubTopics() or[]:

_echo('AttachedSubTopic', topic, 2)

fortopic inroot_topic.getSubTopics(xmind.core.const.TOPIC_DETACHED) or[]:

_echo('DetachedSubtopic', topic, 2)

forrel insheet.getRelationships():

id1, id2 = rel.getEnd1ID(), rel.getEnd2ID()

print('Relationship: [%s] --> [%s]'% (elements.get(id1), elements.get(id2)))

#周遊畫布

forsheet inworkbook.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)

内容來源于網絡,如有侵權請聯系客服删除