2018-09-14 python-xmind文件解析

2018-09-14 python-xmind文件解析

参考地址:

xmind-sdk-python2.x:

github.com/xmindltd/xmi

xmind-sdk-python3.x:

github.com/jmoraleda/xm

安装XMind SDK for python3

git clone https://github.com/jmoraleda/xmind-sdk-python3.git

安装Xmind SDK for python

python3 setup.py install 

文件创建

创建一个新文件

from xmind import save 
from xmind.core.workbook import WorkbookDocument
​
if __name__ == "__main__":
    """
    创建一个空文件
    """
    # 创建xmind workbook
    wb = WorkbookDocument("", "./example.xmind")
    # 保存xmind 文件
    save(wb)

使用加载方法

from xmind import save, load
from xmind.core.workbook import WorkbookDocument 
​
if __name__ == "__main__":
    """ 
    创建一个空文件
    """
    # 创建xmind workbook
    wb = load("./example.xmind")
    # 保存xmind 文件
    save(wb)

workSheet 设定名称

from xmind import save 
from xmind.core.workbook import WorkbookDocument
​
if __name__ == "__main__":
    """
    WorkSheet作成
    """
    # xmind创建WorkBook组成
    wb = WorkbookDocument("", "./example.xmind")
​
    # WorkSheet组成
    ws = wb.getPrimarySheet()
    ws.setTitle("WorkSheet1")
    
    # WorkSheet的Root Topic的获取
    rt = ws.getRootTopic()
    rt.setTitle("root topic")
    
    # Sub Topic添加到Root Topic
    rt.addSubTopic().setTitle("sub topic1")
    
    # 文件保存
    save(wb)

Sub topic 追加到root topic

#!/usr/bin/env python3
from xmind import save
from xmind.core.workbook import WorkbookDocument
​
if __name__ == "__main__":
    """
    sub topic追加到root topic
    """
    # 创建xmind WorkBook
    wb = WorkbookDocument("", "./example.xmind")
​
    # 创建WorkSheet表
    ws = wb.getPrimarySheet()
    ws.setTitle("WorkSheet1")
​
    # 获取WorkSheet的Root Topic并设置标题
    rt = ws.getRootTopic()
    rt.setTitle("root topic")
​
    # 将sub topic 追加到 root topic
    rt.addSubTopic().setTitle("sub topic1")
​
    # 保存xmind文件
    save(wb)

如果要添加多个sub topic,并将它们连续添加到rt对象下。

    # 将sub topic 追加到 root topic
    rt.addSubTopic().setTitle("sub topic1")
    rt.addSubTopic().setTitle("sub topic2")

Topic的层次结构

#!/usr/bin/env python3
from xmind import save
from xmind.core.workbook import WorkbookDocument
​
if __name__ == "__main__":
    """
    在sub topic添加到new topic
    image: root topic --- sub topic1 --- sub topic3
                       |
                        - sub topic2
    """
    # 创建xmind WorkBook
    wb = WorkbookDocument("", "./example.xmind")
​
    # 创建workSheet 
    ws = wb.getPrimarySheet()
    ws.setTitle("WorkSheet1")
​
    # 获取WorkSheet的Root Topic并设置标题
    rt = ws.getRootTopic()
    rt.setTitle("root topic")
​
    # 将sub topic 追加到 root topic
    rt.addSubTopic().setTitle("sub topic1")
    rt.addSubTopic().setTitle("sub topic2")
​
    # 获取sub topic的index
    topic_index = {}
    for t in rt.getSubTopics():
        topic_index[t.getTitle()] = t.getIndex()
​
    # 在 sub topic1 下 添加主题 sub topic3
    rt.getSubTopicByIndex(index=topic_index['sub topic1']).addSubTopic().setTitle("sub topic3")
​
    # 保存xmind文件
    save(wb)

sub topic3下添加主题

#!/usr/bin/env python3
from xmind import save
from xmind.core.workbook import WorkbookDocument
​
if __name__ == "__main__":
    """
    在sub topic添加新 topic 
    image: root topic --- sub topic1 --- sub topic3 ---- sub topic4
                       |
                        - sub topic2
    """
    # 创建xmind WorkBook
    wb = WorkbookDocument("", "./example.xmind")
​
    # 创建WorkSheet
    ws = wb.getPrimarySheet()
    ws.setTitle("WorkSheet1")
​
    # 获取WorkSheet的root topic并设置title
    rt = ws.getRootTopic()
    rt.setTitle("root topic")
​
    # 将Sub Topic追加到Root Topic
    rt.addSubTopic().setTitle("sub topic1")
    rt.addSubTopic().setTitle("sub topic2")
​
    # 获取sub topic的index
    topic_index = {}
    for t in rt.getSubTopics():
        topic_index[t.getTitle()] = t.getIndex()
​
    # 在 sub topic1 下 添加主题 sub topic3
    rt.getSubTopicByIndex(index=topic_index['sub topic1']).addSubTopic().setTitle("sub topic3")
​
    # 在子主题3下添加主题
    for t in rt.getSubTopics():
        if(t.getTitle() == "sub topic1"):
            for st in t.getSubTopics():
                st.addSubTopic().setTitle("sub topic4")
​
    # xmind保存
    save(wb)

读文件

#!/usr/bin/env python3
from xmind import load, save
​
if __name__ == "__main__":
    """
    读取xmind文件
    """
    # 创建xmind workbook 
    wb = load("./example.xmind")
​
    # 获取worksheet 
    ws = wb.getPrimarySheet()
​
    # 获取workSheet的根主题
    rt = ws.getRootTopic()
​
    # 显示子主题
    for topic in rt.getSubTopics():
        print(topic.getTitle())

指定要加载的worksheet

#!/usr/bin/env python3
from xmind import load, save
​
if __name__ == "__main__":
    """
    指定要加载的worksheet 
    """
    # 创建xmind workbook
    wb = load("./example.xmind")
​
    # 获取多个工作表
    wss = wb.getSheets()
​
    # 指定sheet2表
    for ws in wss:
        if(ws.getTitle() == "sheet2"):
            ws = ws
            break
​
    # 获取根主题
    rt = ws.getRootTopic()
​
    # 显示子主题
    for topic in rt.getSubTopics():
        print(topic.getTitle())

将主题添加到现有文件

在下面图中将p添加到a-d。


#!/usr/bin/env python3
from xmind import load, save
​
def getAlltopic(topics, allTopics):
    """
    创建一个字典,返回主题的对象,并将XMind主题的标题作为键
    :type topics: list
    :param topics: xmind主题对象列表
​
    :type allTopics: dict
    :param allTopics: 字典保存主题的对象
​
    :rtype: dict
    :return: 返回topic对象的字典,将topic作为作key
    """
    for topic in topics:
        allTopics[topic.getTitle()] = topic
        if(len(topic.getSubTopics()) > 0):
            getAlltopic(topic.getSubTopics(), allTopics)
​
    return allTopics
​
if __name__ == "__main__":
    """
   添加到现有主题, 但是,此方法假定主题标题不受影响
    """
    # 读取xmind文件
    wb = load("./example.xmind")
    ws = wb.getPrimarySheet()
    rt = ws.getRootTopic()
    # 获取所有主题的索引
    allTopics = {}
    r = getAlltopic(rt.getSubTopics(), allTopics)
​
    # root topic 将“p”添加到a - d
    r['d'].addSubTopic().setTitle("p")
​
    # 保存文件
    save(wb)

文件别名保存

#!/usr/bin/env python3
from xmind import load, save
​
if __name__ == "__main__":
    """
    文件别名保存
    """
    wb = load("./example.xmind")
    wss = wb.getSheets()
    for ws in wss:
        if(ws.getTitle() == "sheet2"):
            ws = ws
            break
    rt = ws.getRootTopic()
    rt.addSubTopic().setTitle("b")
    save(wb, "./hoge.xmind")

demo:

解析xmind文件

import xmind
​
dic = {}
def vistor(root):
    lis = []
    if rootTopic.getSubTopics():
        for topic in rootTopic.getSubTopics():
            lis.append(topic.getTitle())
        dic[rootTopic.getTitle()] = lis
    else:
        dic[rootTopic.getTitle()] = None
​
def dump_topic(root):
    vistor(root)
    for child in root.getSubTopics():
        dump_topic(child)
​
​
def main():
    x = xmind.load('xxxx.xmind')
    for sheet in x.getSheets():
        # 获取根标题
        rootTopic = sheet.getRootTopic()
        dump_topic(root)
​
if __name__ == "__main__":
    main()

发布于 2018-10-27