python3 Json转xmind代码与打开
问题
Json转xmind格式,使用xmind包生成的xmind文件无法使用xmind软件打开。
Json转xmind
首先安装xmind包;
pip install xmind
import xmind
def genXmindByJson(parent, data):
theTree = data
if theTree is None:
return
# 生成一个子节点
node = parent.addSubTopic()
for key in theTree:
print(key)
if key == 'name':
print(theTree['name'])
# 设置子节点的名字(标题)
node.setTitle(theTree['name'])
elif key == 'children':
if len(theTree['children'])>0:
for son_tree in theTree['children']:
genXmindByJson(node,son_tree)
else:
pass
else:
pass
def genXmind(input):
workbook = xmind.load("temp.xmind")
# 开一个画布
sheet = workbook.getPrimarySheet()
# 设置根节点
root = sheet.getRootTopic()
# 设置根节点的名字(标题)
root.setTitle(input['name'])
# root.setStructureClass = "org.xmind.ui.logic.right"
if 'children' in input:
for son_tree in input['children']:
genXmindByJson(root, son_tree)
xmind.save(workbook=workbook, path=input['name'] + '.xmind')
if __name__ == '__main__':
mydata = {
'name': '父',
'children': [
{
'name': '子1',
'children': [
{'name': '子11'}
]
},
{
'name': '子2'
}
]
}
genXmind(mydata)
代码参考:
参考代码
生成文件并打不开
但当我使用XMind打开时,出现如下错误:
后来发现使用XMind8即可解决问题:
XMind8下载地址:
XMind8