zoukankan      html  css  js  c++  java
  • 《Python3:字典(或者Json)数据转成思维导图xmind文件》

    Python3:字典Dict(或者Json)数据转成思维导图xmind文件

     (2018-10-27 09:05:20)
     
    一,环境搭建
    1. xmind模块
    1. Flask模块
    不赘述
     
    二,工程
    该工程通过Flask模块接受来自requests方法post过来的json数据和其他参数,然后在参数指定目录下创建xmind文件。
    源码1:
    import xmind
    # from xmind.core.const import TOPIC_DETACHED
    # from xmind.core.markerref import MarkerId


    def dict_ite(dicts, topic):
    for key in dicts:
    # Create a topic with key
    subtopic = topic.addSubTopic()
    subtopic.setTitle(key)
    if isinstance(dicts[key], dict): # if the key is a dict, then
    dict_ite(dicts[key], subtopic)
    return True


    def dict2xmind(dicts, filename, path):
    file_str = path + "/" +filename+".xmind"
    workbook = xmind.load(file_str) # load an existing file or create a new workbook if nothing is found

    sheet = workbook.getPrimarySheet() # get the first sheet
    sheet.setTitle(filename) # set its title
    rtopic_sheet = sheet.getRootTopic() # get the root topic of this sheet
    rtopic_sheet.setTitle(filename) # set its title

    if dict_ite(dicts, rtopic_sheet):
    xmind.save(workbook, file_str)
    return True
    else:
    return False

    if __name__ == '__main__':
    Temp ={"H1":1,"H2":2,"H3":{"H21":1,"H22":22,"H23":{"H31":1}}}
    dict2xmind(Temp, "Kitty1", path="")

    源码2:
    # from flask import Flask, g
    from flask import Flask, jsonify, request, flash, logging, session, redirect, url_for
    from dump import dict2xmind

    __all__ = ['app']

    app = Flask(__name__)


    @app.route('/')
    def index():
    return '

    Welcome to XMind in Python3

    '


    @app.route('/get', methods=['POST'])
    def get_xmind_file():
    if request.method == "POST":
    res = request.get_json()
    status = False
    if "data_json" and"path" and "name" in res.keys():
    status = dict2xmind(dicts=res["data_json"], filename=res["name"], path=res["path"])
    else:
    return jsonify(code=200, status=status, message='ok', data="Missing Infos.")
    return jsonify(code=200, status=status, message='ok', data="Successfully Generating File.")


    if __name__ == '__main__':
    # API地址和端口
    API_HOST = '0.0.0.0' # http://127.0.0.1
    API_PORT = 5005
    app.run(host=API_HOST, port=API_PORT)

    """Example in Clients:
    Temp ={"H1":1,"H2":2,"H3":{"H21":1,"H22":22,"H23":{"H31":1}}}
    res = {"data_json": Temp,"path": "E:XMindAPI","name": "DR"}
    result = requests.post("http://localhost:5005/get", json=res) # 或者http://127.0.0.1:5005
    print(result.text)
    """
  • 相关阅读:
    垃圾回收机制_合集
    线程_同步应用
    动态给类的实例对象 或 类 添加属性
    【Python】画一个心形
    【JS】网站运行时间
    【Python】random库
    【HTML】iframe嵌套界面自适应,可高度自由收缩
    【HTML】三种方法使HTML单页面输入密码才能访问
    维护
    投喂记录
  • 原文地址:https://www.cnblogs.com/cx2016/p/12757645.html
Copyright © 2011-2022 走看看