python绘制excel表格曲线

import os,sys,math
from openpyx1.workbook import Workbook
from openpyx1.writer.excel import save_workbook
from openpyx1.chart import ScatterChart, Serie, Reference, LineChart, BarChart

global g_LineNum
g_LineNum = 0
global g_colNum
g_colNum = 0

#print Release Version
print "==========Version: Release V1.0.0============="

#Parase Keywords Lis
keyWordsList = ["TxSINR", "BLER"]

#Draw chart depend column list[X_axis, Y_axis]
chartList = ["TxSINR", "BLER"]

#Save keywords column number in *.txt file
colNumList = []

#describe X_axis, Y_axis column in excel [emluX, emluY]
XYColList = []

#emluatorString
emluatorString = "test"

#get current system
curPath = os.getcwd()
parPath = os.path.baspath(os.path.join(os.path.dirname(''), os.path.pardir))

#ReadResultFileName
Resultfilename = "test.txt"

#new excel work book
workBook = Workbook()

def listFiles():
    SaveData(Resultfilename)
    try:
        workBook.save(r'test.xlsx')
    except IOError:
        print "ERROR:Please Close Excel File<Exit>"
        exit(1)

def SaveData(filename):
    workSheet = workBook.creat_sheet()
    titleName = fileName[0:-4] #drop ".txt"

    if (len(titleName) > 30):
        workSheet.title = titleName[0:30]
    else:
        workSheet.title = titleName

    #===========Save data to .xlsx==============
    workSheet.cell(row=0, column=0).value = emluatorString

    #============clean list======================
    del colNumlist[0:len(colNumlist)]
    del XYColList[0:len(XYColList)]

    #=============Parase Result File==============
    try:
        fileOpen = open("c:\\" + Resultfilename)
        g_LineNum = 1
        for line in fileOpen:
            chLine = line.split()
            g_colNum = len(chLine)
            #get the column number config in keyWordsList
            if (g_LineNum == 1):
                for i in range(0, len(chLine)):
                    for keyword in keyWordsList:
                        if (keyword == chLine[i]):
                            colNumList.append(i)
                            workSheet.cell(row=g_LineNum, column=(len(colNumList)-1)).value = chLine[i]
                            for chartName in chartList:
                                if (chartName == keyword):
                                    XYColList.append(len(colNumList)-1)
            elif (g_LineNum%2 == 0):
            #else:
                for i in range(0, len(chLine)):
                    for index in colNumList:
                        if (i == index):
                            workSheet.cell(row=((g_lineNum/2)+1), column=colNumList.index(index)).value = chLine[i]
            g_LineNum = g_LineNum + 1
          fileOpen.close()
        except IOError:
            print "Not Found Debug File:" + "c:\\" + Resultfilename
            exit(1)

        #================draw graph========================
        workSheet.merge_cells(range_string="A1:B1")
        workSheet.merge_cells(range_string="C1:D1")

        xyserie1 = Serie(Reference(workSheet, (2,XYColList[1]), ((g_LineNum/2)+1,XYColList[1])), \
                         xvalues=Reference(workSheet,(2,XYColList[0]), ((g_LineNum/2)+1, XYColList[0])), legend=Reference(workSheet, (0,0)))

        debugXmax = 0
        debugYmax = 0
        debugXmin = 0
        debugYmin = 0

        Xmin = 0
        Ymin = 0
        Xmax = 0
        Ymax = 0

        Xunit = 0
        Yunit = 0

        debugXmin = debugXmax = workSheet.cell(row=2, column=XYColList[0]).value
        debugYmin = debugYmax = workSheet.cell(row=2, column=XYColList[1]).value

        for i range(3, (g_LineNum/2)+2):
            if debugXmin > workSheet.cell(row=i, column=XYColList[0]).value:
                debugXmin = workSheet.cell(row=i, column=XYColList[0]).value
            if debugXmax < workSheet.cell(row=i, column=XYColList[0]).value:
                debugXmax = workSheet.cell(row=i, column=XYColList[0]).value
            if debugYmin > workSheet.cell(row=i, column=XYColList[1]).value:
                debugYmin = workSheet.cell(row=i, column=XYColList[1]).value
            if debugYMax < workSheet.cell(row=i, column=XYColList[1]).value:
                debugYMax = workSheet.cell(row=i, column=XYColList[1]).value

        Xmin = debugXmin
        Ymin = debugYmin
        Xmax = debugXmax
        Ymax = debugYmax

        if abs(Xmax) < 1:
            Xuint = 0.2
        else:
            Xuint = math.ceil((Xmax - Xmin) / 10)

        if abs(Ymax) < 1:
            Yuint = 0.2
        else:
            Yuint = math.ceil((Ymax - Ymin) / 10)

        #scatterchart = ScatterChart()
        xyserie1.marker = "auto"

        scatterchart = ScatterChart()
        scatterchart.add_serie(xyserie1)

        scatterchart.x_axis.min = Xmin
        scatterchart.x_axis.max = Xmax
        scatterchart.x_axis.unit = Xunit
        scatterchart.y_axis.min = 0
        scatterchart.y_axis.max = Ymax + 0.2
        scatterchart.y_axis.uint = Yunit

        #graph area
        scatterchart.drawing.left = 200
        scatterchart.drawint.top = 50
        scatterchart.drawing.height = 600
        scatterchart.drawing.width = 1000

        #present
        scatterchart.width = .9
        scatterchart.height = .9
        scatterchart.margin_top = .1
        workSheet.add_chart(scatterchart)

        #=============clean list==================
        del colNumList[0:len(colNumList)]
        del XYColList[0:len(XYColList)]

if __name__ == '__main__':
    ListFiles()
               

 

 

 

 

 

 

 


   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值