Python 使用Python操作xmind文件

简介: Python 使用Python操作xmind文件

使用Python操作xmind文件


 

 

测试环境

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)

 

目录
相关文章
|
20天前
|
机器学习/深度学习 存储 算法
解锁文件共享软件背后基于 Python 的二叉搜索树算法密码
文件共享软件在数字化时代扮演着连接全球用户、促进知识与数据交流的重要角色。二叉搜索树作为一种高效的数据结构,通过有序存储和快速检索文件,极大提升了文件共享平台的性能。它依据文件名或时间戳等关键属性排序,支持高效插入、删除和查找操作,显著优化用户体验。本文还展示了用Python实现的简单二叉搜索树代码,帮助理解其工作原理,并展望了该算法在分布式计算和机器学习领域的未来应用前景。
|
1月前
|
监控 网络安全 开发者
Python中的Paramiko与FTP文件夹及文件检测技巧
通过使用 Paramiko 和 FTP 库,开发者可以方便地检测远程服务器上的文件和文件夹是否存在。Paramiko 提供了通过 SSH 协议进行远程文件管理的能力,而 `ftplib` 则提供了通过 FTP 协议进行文件传输和管理的功能。通过理解和应用这些工具,您可以更加高效地管理和监控远程服务器上的文件系统。
58 20
|
1月前
|
存储 数据采集 数据处理
如何在Python中高效地读写大型文件?
大家好,我是V哥。上一篇介绍了Python文件读写操作,今天聊聊如何高效处理大型文件。主要方法包括:逐行读取、分块读取、内存映射(mmap)、pandas分块处理CSV、numpy处理二进制文件、itertools迭代处理及linecache逐行读取。这些方法能有效节省内存,提升效率。关注威哥爱编程,学习更多Python技巧。
|
1月前
|
存储 JSON 对象存储
如何使用 Python 进行文件读写操作?
大家好,我是V哥。本文介绍Python中文件读写操作的方法,包括文件读取、写入、追加、二进制模式、JSON、CSV和Pandas模块的使用,以及对象序列化与反序列化。通过这些方法,你可以根据不同的文件类型和需求,灵活选择合适的方式进行操作。希望对正在学习Python的小伙伴们有所帮助。欢迎关注威哥爱编程,全栈路上我们并肩前行。
|
1月前
|
存储 算法 Serverless
剖析文件共享工具背后的Python哈希表算法奥秘
在数字化时代,文件共享工具不可或缺。哈希表算法通过将文件名或哈希值映射到存储位置,实现快速检索与高效管理。Python中的哈希表可用于创建简易文件索引,支持快速插入和查找文件路径。哈希表不仅提升了文件定位速度,还优化了存储管理和多节点数据一致性,确保文件共享工具高效运行,满足多用户并发需求,推动文件共享领域向更高效、便捷的方向发展。
|
2月前
|
计算机视觉 Python
如何使用Python将TS文件转换为MP4
本文介绍了如何使用Python和FFmpeg将TS文件转换为MP4文件。首先需要安装Python和FFmpeg,然后通过`subprocess`模块调用FFmpeg命令,实现文件格式的转换。代码示例展示了具体的操作步骤,包括检查文件存在性、构建FFmpeg命令和执行转换过程。
82 7
|
4月前
|
自然语言处理 数据处理 Python
python操作和解析ppt文件 | python小知识
本文将带你从零开始,了解PPT解析的工具、工作原理以及常用的基本操作,并提供具体的代码示例和必要的说明【10月更文挑战第4天】
715 60
|
4月前
|
安全 Linux 数据安全/隐私保护
python知识点100篇系列(15)-加密python源代码为pyd文件
【10月更文挑战第5天】为了保护Python源码不被查看,可将其编译成二进制文件(Windows下为.pyd,Linux下为.so)。以Python3.8为例,通过Cython工具,先写好Python代码并加入`# cython: language_level=3`指令,安装easycython库后,使用`easycython *.py`命令编译源文件,最终生成.pyd文件供直接导入使用。
155 3
python知识点100篇系列(15)-加密python源代码为pyd文件
|
4月前
|
Linux 区块链 Python
Python实用记录(十三):python脚本打包exe文件并运行
这篇文章介绍了如何使用PyInstaller将Python脚本打包成可执行文件(exe),并提供了详细的步骤和注意事项。
197 1
Python实用记录(十三):python脚本打包exe文件并运行
|
3月前
|
开发者 Python
Python中__init__.py文件的作用
`__init__.py`文件在Python包管理中扮演着重要角色,通过标识目录为包、初始化包、控制导入行为、支持递归包结构以及定义包的命名空间,`__init__.py`文件为组织和管理Python代码提供了强大支持。理解并正确使用 `__init__.py`文件,可以帮助开发者更好地组织代码,提高代码的可维护性和可读性。
186 2

热门文章

最新文章