使用python進行excel自動化操作

使用Python一鍵生成Oracle性能excel曲線圖

1、cx_Oracle安裝與使用

1.1 python 使用最新3.5.1 64位

1.2 Oracle 使用11g 32位

1.3 cx_Oracle使用5.2.1 與11g的Oracle與python一致

在cx_Oracle下載安裝使用時候注意事項:

  • 必須與選用的python與Oracle版本一致
  • 當遇到報錯,則需要冷靜看一下 各服務(wù) 是否已經(jīng)開啟

1.4 cx_Oracle使用

import cx_Oracle
conn=cx_Oracle.connect('JGANG/JGANG@DATABASE')  #建立Oracle連接
cursor=conn.cursor()                            #建立Cursor光標,用此執(zhí)行SQL語句
cursor.execute('select distinct t.stationcode from TARGET_STATION t')
#執(zhí)行SQL語句
row=cursor.fetchall()       #調(diào)用cursor.fetchall()一次取完,cursor.fetchone()一次取一行

def GetInOutFlow(staName):    #根據(jù)傳入的車站名得到對應(yīng)的車站進出站量
    sqlstr="select t.time,t.innum,t.outnum from TARGET_STATION T where t.stationcode='{0}' order by t.time".format(staName)
    cursor.execute(sqlstr)
    InOutFlow=cursor.fetchall()    #取出對應(yīng)車站的進出站量
    return InOutFlow

for x in row:
    staName=x[0]        #車站名字
    InOutFlow=GetInOutFlow(staName)
    print(InOutFlow)            #不要直接打印將其保存到excel中
    break

2、excel操作使用xlsxwriter模塊

2.1 Python的Excel處理模塊:XlsxWriter下載

2.2 XlsxWriter下載與安裝

  • cmd 下 輸入 pip install wheel
  • 到XlsxWriter.whl所在文件下,pip install 包名字.whl

2.3 XlsxWriter使用,使用看這

workbook  = xlsxwriter.Workbook('filename.xlsx')
worksheet = workbook.add_worksheet()  #()中可以加入名字

worksheet.write(0, 0, 'Hello Excel')

workbook.close()

2.4 制作折線圖

import cx_Oracle
import xlsxwriter as xls
conn=cx_Oracle.connect('JGANG/JGANG@DATABASE')  #建立Oracle連接
cursor=conn.cursor()                            #建立Cursor光標,用此執(zhí)行SQL語句
cursor.execute('select distinct t.stationcode from TARGET_STATION t')
#執(zhí)行SQL語句
row=cursor.fetchall()       #調(diào)用cursor.fetchall()一次取完,cursor.fetchone()一次取一行
#xlsx文件操作
workbook=xls.Workbook('車站進出站信息.xlsx')
def GetInOutFlow(staName):    #根據(jù)傳入的車站名得到對應(yīng)的車站進出站量
    sqlstr="select t.time,t.innum,t.outnum from TARGET_STATION T where t.stationcode='{0}' order by t.time".format(staName)
    cursor.execute(sqlstr)
    InOutFlow=cursor.fetchall()    #取出對應(yīng)車站的進出站量
    return InOutFlow
def Transposition(InOutFlow):          #將InOutFlow轉(zhuǎn)置,并放回
    result=[]
    colTime=[]
    colIn=[]
    colOut=[]
    for row in InOutFlow:
        colTime.append(row[0])
        colIn.append(row[1])
        colOut.append(row[2])
    result.append(colTime)
    result.append(colIn)
    result.append(colOut)
    return result

def Export(InOutFlow,staName):             #將車站的進出站信息保存到xlsx中
    worksheet=workbook.add_worksheet(staName)
    bold=workbook.add_format({'bold':1})
    #添加表頭
    headings=['時間','進站','出站']
    worksheet.write_row('A1',headings,bold)
    #添加內(nèi)容
    worksheet.write_column('A2',InOutFlow[0])
    worksheet.write_column('B2',InOutFlow[1])
    worksheet.write_column('C2',InOutFlow[2])
    #創(chuàng)建新的圖表,折線圖
    chart=workbook.add_chart({'type':'line'})
    chart.add_series(
        {
         'name':       '={0}!$B$1'.format(staName),
         'categories': '={0}!$A$2:$A${1}'.format(staName,len(InOutFlow[0])+1),
         'values':     '={0}!$B$2:$B${1}'.format(staName,len(InOutFlow[0])+1),
         })
    chart.add_series(
        {
        'name':   [staName,0,2],
        'categories':[staName,1,0,len(InOutFlow[0])+1,0],
        'values':[staName,1,2,len(InOutFlow[0])+1,2],
        })
    #添加表頭和xy
    chart.set_title({'name':'{0}站全天進出站量變化趨勢'.format(staName)})
    chart.set_x_axis({'name':'時間'})
    chart.set_y_axis({'name':'人數(shù)(人)'})
    #Set an Excel chart style. Colors with white outline and shadow.
    chart.set_style(10)
    # Insert the chart into the worksheet (with an offset).
    worksheet.insert_chart('E2', chart, {'x_offset': 25, 'y_offset': 10})


for x in row:
    staName=x[0]        #車站名字
    InOutFlow=Transposition(GetInOutFlow(staName))
    #print(InOutFlow)            #不要直接打印將其保存到excel中
    Export(InOutFlow,staName)
workbook.close()

3、讀取excel內(nèi)容,xlrd下載

3.1 xlrd安裝教程

下載xlrd文件后,解壓到一個文件夾,然后cd到該文件夾,python setup.py install 進行安裝

3.2 使用

import xlrd
#讀取xlxs文件,將車站保存到內(nèi)存
filename=r'{0}\車站名.xlsx'.format(currentPath)
data = xlrd.open_workbook(filename)
sheetname = data.sheet_names()
sheet = data.sheet_by_index(0)
rows = sheet.nrows
cols = sheet.ncols
staName=[]
for row in range(rows):
   staName.append(sheet.row_values(row)[0])

4、結(jié)果展示

進出站變化曲線
全天斷面分析
滿載率變化

5、Python操作excel優(yōu)勢

5.1 python本身語法簡單,作為一門腳本語言可以很方便的進行操作

5.2 python的excel模塊強大,直接生成excel圖和內(nèi)容插入

5.3 若要做好幾百張類似的圖,不用再跟傻瓜一樣去拉圖啦?。?!

5.4 可重復使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容