如何把幕布数据转为excel cvs格式数据

2021/2/2 17:19 阅读 62 views 次 评论 0 条

我的数据只用到两级,并且二级合并到一个单元格里,二级第一行数据会被写到excel第一列.

幕布需要先切换到思维导图模式,然后导出freemind的.mm格式,再用以下代码处理

代码仅做记录用,如果你有类似的需求,请参考以下原代码

代码参考自:http://sunyongfeng.com/201704/programmer/python/xmind2excel

 

# coding=gbk

#!/usr/bin/python

 

import xml.etree.ElementTree as ET

import xlwt

import argparse

import pysnooper

 

g_row = 0

g_col = 0

g_name = ""

 

def do_write_excel(text, row, col):

    #print text + " row %d + col %d"%(row, col)

    ws.write(row, col, text) 

 

@pysnooper.snoop()

def perf_func(elem, func, level = 0):

    global g_row

    global g_name

    func(elem, g_row, level)

 

    for child in list(elem):

        name = child.get('TEXT')

        

        perf_func(child, func, level + 1)  

        if child.find('node') is None and name is not None:

                g_row = g_row + 1                

 

              

    

def write_excel(elem, row, level):

    name = elem.get('TEXT')

    if name is not None:

        do_write_excel(name, row, level)

 

parser = argparse.ArgumentParser()

 

parser.add_argument('-i', '--input-file', type=str, dest='inputfile', required=True)

parser.add_argument('-o', '--output-file', type=str, dest='outputfile', default='freemind2excel.xls', help='Default outputfile is freemind2excel.xls')

args = parser.parse_args()

 

if args.inputfile is None:

    parser.print_help()

    exit()

 

root = ET.parse(args.inputfile)

map_version = root.getroot()

first_node = map_version.find('node')

 

wb = xlwt.Workbook()

ws = wb.add_sheet('freemind2excel')

 

#perf_func(first_node, write_excel) #递归遍历打印

for child in list(first_node):

    g_row += 1;

    i = 0;

    name = child.get('TEXT')

    do_write_excel(name, g_row, 1)

    for child_child in list(child):

        child_name = child_child.get('TEXT')

        if i == 0:

            do_write_excel(child_name, g_row, 0)#写keywords到第一列

        else:

            g_name += "\n"+child_name

        i += 1

    do_write_excel(g_name, g_row, 2)

    g_name = ""

    i = 0;

wb.save(args.outputfile)

 



版权声明:本文著作权归原作者所有,欢迎分享本文,谢谢支持!
转载请注明:如何把幕布数据转为excel cvs格式数据 | 沙聚塔

发表评论


表情