Python 使用Python操作xmind文件
测试环境
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)
本文转自:https://www.cnblogs.com/shouke/p/12685235.html
上一篇: Python爬虫实践,获取百度贴吧内容
下一篇: 运维管理后台