python实现xmind_Python 使用Python操作xmind文件

该博客介绍了如何使用Python的xmind库来加载、创建、更新和解析XMind思维导图文件。示例代码展示了如何添加主题、子主题、自由主题、超链接、标记、注释等元素,以及如何保存和另存为新的XMind文件。此外,还提供了自定义解析XMind文件的方法,展示如何遍历并打印导图内容。
摘要由CSDN通过智能技术生成

测试环境

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 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()

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')

运行结果

解析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)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值