使用Python操作xmind文件

by:授客 QQ1033553122

 

测试环境

Win10

Python 3.5.4

XMind-1.2.0.tar.gz

下载地址:

https://files.pythonhosted.org/packages/7c/8c/e13a82fa9b0394c0d58248196d7d51d7274407cdebc1df36b76034ab990d/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.xmlcomments.xmlstyles.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)

Python 使用Python操作xmind文件的更多相关文章

  1. Python—集合的操作、文件的操作

    1.集合的操作 2.文件的操作 1.集合的操作 定义: 1.不同元素组成,自动去重 2.无序 3.集合中的元素必须是不可变类型 1.集合的定义: >>> s1 = set('abcd ...

  2. python基础-PyYaml操作yaml文件

    yaml语法 格式 它的基本语法规则如下 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使用空格. 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 YAML 支持的数据结构有 ...

  3. python使用xlrd操作Excel文件

    一.xlrd读取Excel文件 用xlrd进行读取比较方便,流程和平常手动操作Excel一样,打开工作簿(Workbook),选择工作表(sheets),然后操作单元格(cell). 例子:要打开当前 ...

  4. Python之Pandas操作csv文件dataframe

    # -*- coding: utf-8 -*- # author:baoshan import pandas as pd def main(): aqi_data = pd.read_csv('chi ...

  5. (Python )格式化输出、文件操作、json

    本节学习Python的格式化输出,文件操作以及json的简单用法 1.格式化输出 将非字符串类型转换成字符串,可以使用函数:str() 或者repr() ,(这两个函数的区别目前我还没搞懂,求解答) ...

  6. 使用 Python 进行稳定可靠的文件操作

    程序需要更新文件.虽然大部分程序员知道在执行I/O的时候会发生不可预期的事情,但是我经常看到一些异常幼稚的代码.在本文中,我想要分享一些如何在Python代码中改善I/O可靠性的见解. 考虑下述Pyt ...

  7. Python操作Zip文件

    Python操作Zip文件 需要使用到zipfile模块 读取Zip文件 随便一个zip文件,我这里用了bb.zip,就是一个文件夹bb,里面有个文件aa.txt. import zipfile # ...

  8. Python系列-python文件操作

    原链接:https://blog.csdn.net/m0_37745438/article/details/79573414 python提供了一系列方法来对文件进行读取.写入等操作 一.打开文件的方 ...

  9. python的学习笔记01_5文件操作

    一,文件操作基本流程. 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众 ...

  10. python 操作Excel文件

    1   安装xlrd.xlwt.xlutils cmd下输入: pip install xlrd        #读取excel pip install xlwt        #写入excel pi ...

随机推荐

  1. mp4封装格式与MPEG4Extractor

    首先来看mp4的封装格式,mp4数据都被放在一个个的箱子当中,也就是box,box的字节序为网络字节序,也就是大端存储,box由header和body组成,header指明box的大小和类型,body ...

  2. HashMap设置初始容量一直都用错了?

    1 背景 今天在代码审查的时候,发现一位离职的同事留下了这样一串代码: Map<String,String> map = new HashMap<>((int)(list.si ...

  3. latex图片格式问题解决(viso转PDF转eps)

    latex图片格式问题解决(viso->PDF->eps) 1.viso绘图 如何让界面自动适应图的大小? 设计->大小->适应绘图 如何将VISIO图转换为PDF? 文件-& ...

  4. Centos7安装Nginx教程,一步安装http和https

    nginx是一款轻量级web服务器,主要有负载均衡和反向代理的特性. 安装准备 nginx一些模块需要依赖lib库,所以先安装lib库,执行以下命令: [root@localhost local]# ...

  5. Linux扩展篇-shell编程(五)-流程控制(三)-for语句

    基本语法: 格式一 for(( 初始值; 循环控制条件; 变量变化)) do statements done 格式二 for 变量 in 值1 值2 值3 ... do statements done ...

  6. Java中常见的几种的溢出

    引言 在开发过程中,因为编程经验不足,经常会导致各种各样的溢出,今天本文就举例说明几种常见的溢出 堆溢出 堆溢出是最常见的一种溢出. 导致原因:堆中没有足够的空间储存新生成的实例对象 public s ...

  7. 使用shell脚本在Linux中管理Java应用程序

    目录 前言 一.目录结构 二.脚本实现 1. 脚本内容 2. 使用说明 2.1 配置脚本 2.2 脚本部署 2.3 操作你的Java应用 总结 前言 在日常开发和运维工作中,管理基于Java的应用程序 ...

  8. Json输出List集合对象和map对象 JSON格式

    Json输出List集合对象和map对象 JSON格式 //Json输出List集合对象 [{"属性1":["值1"],"属性2":&quo ...

  9. Tarjan 求有向图的强连通分量

    重温Tarjan, 网上看了许多博客感觉都讲的不清楚. 故传上来自己的笔记, 希望帮到大家. 提到的一些概念可以参考 oi wiki, 代码也是 oi wiki 的, 因为我不认为我能写出比大佬更好的 ...

  10. Android自动化-如何获取视图元素属性?

    在做Android自动化时候,我们需要知道视图有哪些元素,元素都有哪些属性,获取到属性我们才能获取到元素从而做自动化控制,所以做Android自动化获取元素属性是必要的第一步 获取视图元素属性最便捷的 ...