python 实现java方法名到excel文件

 整理设计文档时,IF sheet是必须的

用python可以实现从方法名从java文件到excel文件的转换

注意

运行# python methodtoexcel.py -i <inputfile> -o <outputfile>

methodtoexcel.py 是我自己给源代码文件取的名字,你应改为自己取的名字

python2.×环境

xlwt库是需要下载的xlwt库

该库只支持xls格式

目前只能支持java到excel

大概效果

python 实现java方法名到excel文件

源码如下,可以根据自己的要求修改

也想请教如何判断C/C++中的方法名

#!/usr/bin/python

import re
import xlwt
import sys, getopt

def getfilename():
    filelist = ['', '', 'java']
    inputfile = ''
    outputfile = ''
    try:
        opts, args = getopt.getopt(sys.argv[1:],"hi:o:",["ifile=","ofile="])
    except getopt.GetoptError:
        print 'methodToExcel.py -i <inputfile> -o <outputfile>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'methodToExcel.py -i <inputfile> -o <outputfile>'
            sys.exit()
        elif opt in ("-i", "--ifile"):
            isjava = re.match(".*\.java", arg)
            iscpp = re.match(".*\.cpp", arg)
            isc = re.match(".*\.c", arg)
            if not isjava or iscpp or isc:
                print 'exaction support only for java or C/C++ files'
                sys.exit()
            elif isjava:
                filelist[2] = 'java'
            elif iscpp:
                filelist[2] = 'cpp'
            elif isc:
                filelist[2] = 'c'
            filelist[0] = arg
        elif opt in ("-o", "--ofile"):
            isxls = re.match(".*\.xls", arg)
            if not isxls:
                print 'methods can only be exacted to a .xls file'
                sys.exit()
            filelist[1] = arg
    return filelist

def javamethodtoexcel(inputfile, outputfile):
    Fb = open(inputfile, "r")
    wb = xlwt.Workbook()
    ws = wb.add_sheet('Interface')
    ws.write(0, 1, "Module")
    ws.write(0, 2, "FunctionName")
    ws.write(0, 3, "Param")
    ws.write(0, 4, "Return")
    ws.write(0, 5, "Remarks")
    row = 0
    for publicmethod in Fb.readlines():
        if "public" in publicmethod:
            x = re.match(".*\ (.*)\ (.*)\((.*)\).*", publicmethod)
            if x:
                row += 1
                ws.write(row, 4, x.group(1))
                ws.write(row, 2, x.group(2))
                ws.write(row, 3, x.group(3))
    Fb.close()
    wb.save(outputfile)


if __name__ == "__main__":
    filelist = ['', '', '']
    filelist = getfilename()
    if "java" in filelist[2]:
      javamethodtoexcel(filelist[0], filelist[1])