使用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é)果展示
進出站變化曲線
全天斷面分析
滿載率變化