首先需要pip3 install xmind.
更多xmind用法可访问:https://pypi.org/project/XMind/
#coding: utf-8
#author: Alien
#date : 2021年3月9日
import xmind
from xmind.core.markerref import MarkerId
from xmind.core import const
KitiMarkerValue = {
'priority-1': 1,
'priority-2': 2,
'priority-3': 3,
'priority-4': 4,
'priority-5': 5,
'priority-6': 6,
'priority-7': 7,
'priority-8': 8,
'priority-9': 9,
'task-start': 1,
'task-oct': 2,
'task-quarter': 3,
'task-3oct': 4,
'task-half': 5,
'task-5oct': 6,
'task-3quar': 7,
'task-7oct': 8,
'task-done': 9,
}
def decode_xmind(path):
'''XMIND转成JSON
'''
workbook = xmind.load(path)
sheet = workbook.getPrimarySheet()
xmind_data = sheet.getData()
return {
'root': gen_kiti_json(xmind_data.get('topic')),
'template': 'default',
'theme': 'fresh-blue',
'version': '1.4.43'
}
def gen_kiti_json(xmind_topic):
kiti_topic = {}
xmind_id = xmind_topic.get('id')
xmind_title = xmind_topic.get('title')
xmind_note = xmind_topic.get('note')
xmind_link = xmind_topic.get('link')
xmind_markers = xmind_topic.get('markers')
xmind_children = xmind_topic.get('topics', [])
kiti_topic['data'] = {'id': xmind_id}
if xmind_title: kiti_topic['data']['text'] = xmind_title
if xmind_note: kiti_topic['data']['note'] = xmind_note
if xmind_link: kiti_topic['data']['hyperlink'] = xmind_link
for xmind_marker in xmind_markers:
if 'priority' in xmind_marker:
kiti_topic['data']['priority'] = KitiMarkerValue.get(xmind_marker)
elif 'task' in xmind_marker:
kiti_topic['data']['progress'] = KitiMarkerValue.get(xmind_marker)
kiti_topic['children'] = []
for xmind_child in xmind_children:
kiti_topic['children'].append(gen_kiti_json(xmind_child))
return kiti_topic
def encode_xmind(kity_data):
'''JSON转成XMIND
'''
workbook = xmind.load('demo.xmind')
sheet = workbook.getPrimarySheet()
sheet.setTitle('sheet')
root_topic = sheet.getRootTopic()
title = gen_xmind_topic(root_topic, kity_data.get('root'))
file_name = '%s.xmind' % title
# 除了修改记录,其他内容都保存,因为XMind文件的修改记录文件夹比较大,以便节约内存(推荐)
xmind.save(workbook=workbook, path=file_name, except_revisions=True)
return file_name
def gen_xmind_topic(parent_topic, kity_data):
kity_id = kity_data.get('data').get('id')
kity_text = kity_data.get('data').get('text') #文本
kity_priority = kity_data.get('data').get('priority') #优先级
kity_progress = kity_data.get('data').get('progress') #进度
#kity_resource = kity_data.get('data').get('resource') #不知道kity上面是什么鬼
kity_note = kity_data.get('data').get('note') #备注
kity_link = kity_data.get('data').get('hyperlink') #链接
#kity_link_title = kity_data.get('data').get('hyperlinkTitle') #链接名称。xmind好像没有对应方法
kity_children = kity_data.get('children', []) #子主题
parent_topic.setAttribute(const.ATTR_ID, kity_id)
if kity_text: parent_topic.setTitle(kity_text)
if kity_priority: parent_topic.addMarker(getattr(MarkerId, 'priority%s' % kity_priority))
if kity_progress: parent_topic.addMarker(getattr(MarkerId, 'task%s_8' % (kity_progress - 1)))
if kity_note: parent_topic.setPlainNotes(kity_note)
if kity_link: parent_topic.setURLHyperlink(kity_link)
for kity_child in kity_children:
child_topic = parent_topic.addSubTopic()
gen_xmind_topic(child_topic, kity_child)
return kity_text