本文将对我用到过的解析xmind的python包,及对包的一些优化点进行一个分享和记录。
前言
xmindparser是一个可以将xmind文件转换为可变成数据类型的python工具,支持将xmind文件解析为dict、json、xml数据类型,还支持解析为json文件。
安装
- pip安装
pip install xmindparser
- git安装
链接: git地址
示例
-
xmind文件示例:
-
解析代码
import xmindparser
# xmindparser配置
xmindparser.config = {
'showTopicId': True, # 是否展示主题ID
'hideEmptyValue': True # 是否隐藏空值
}
filePath = '/你自己的文件路径/示例.xmind'
# 解析成json数据类型
content = xmindparser.xmind_to_json(filePath)
# 解析成dict数据类型
# content = xmindparser.xmind_to_dict(filePath)
# 解析成xml数据类型
# content = xmindparser.xmind_to_xml(filePath)
# 解析到文件
# content = xmindparser.xmind_to_file(filePath, 'json')
# content = xmindparser.xmind_to_file(filePath, 'xml')
print(content)
- 解析结果
[
{
'title': '画布 1',
'topic': {
'title': '中心主题',
'topics': [
{
'title': '分支主题 1',
'makers': [
'priority-1'
],
'topics': [
{
'title': '子主题 1',
'note': '备注'
},
{
'title': '子主题 2',
'labels': [
'标签1',
'标签2'
]
}
]
},
{
'title': '分支主题 2',
'makers': [
'priority-2'
],
'topics': [
{
'title': '子主题 3',
'makers': [
'smiley-laugh',
'task-done'
],
'link': 'https://www.baidu.com/'
}
]
}
]
},
'structure': 'org.xmind.ui.map.unbalanced'
}
]
从解析结果可以看到,xmindparser可以将节点名称、备注、链接、图标、标签、标注这些都可以解析出来。
但结果里边的structure我并不需要,并且节点间的连线关系也没有显示出来,因此我对xmindparser源码进行了小部分优化。
优化
主要是在配置项中增加了两个配置
# xmindparser配置
xmindparser.config = {
'showTopicId': False, # 原有配置
'hideEmptyValue': True # 原有配置
'showStructure': False, # 新增配置,是否展示结构值
'showRelationship': True # 新增配置,是否展示节点关系
}
优化后的xmindparser包链接: 资源地址
采用优化后的xmindparser包后,解析结果示例:
[
{
'title': '画布 1',
'topic': {
'title': '中心主题',
'topics': [
{
'title': '分支主题 1',
'makers': [
'priority-1'
],
'topics': [
{
'title': '子主题 1',
'note': '备注'
},
{
'title': '子主题 2',
'labels': [
'标签1',
'标签2'
]
}
]
},
{
'title': '分支主题 2',
'makers': [
'priority-2'
],
'topics': [
{
'title': '子主题 3',
'makers': [
'smiley-laugh',
'task-done'
],
'link': 'https://www.baidu.com/'
}
]
}
]
},
'relationships': [
{
'end1Id': 'b4c140d3-b392-4bc3-a4a4-1132da876f0f', #节点ID(唯一),showTopicId设置为True时,可找到对应的节点
'end2Id': 'a28f8c7e-0000-442e-9a32-fdc0f9d5ef89'
}
]
}
]