熱線電話:13121318867

登錄
首頁精彩閱讀python實現報表自動化詳解
python實現報表自動化詳解
2018-04-17
收藏

python實現報表自動化詳解

本篇文章將介紹:
xlwt 常用功能
xlrd 常用功能
xlutils 常用功能
xlwt寫Excel時公式的應用
xlwt寫入特定目錄(路徑設置)
xlwt Python語言中,寫入Excel文件的擴展工具??梢詫崿F指定表單、指定單元格的寫入。支持excel03版到excel2013版。使用時請確保已經安裝python環境
xlrd Python語言中,讀取Excel的擴展工具??梢詫崿F指定表單、指定單元格的讀取。使用時請確保已經安裝python環境。
NOTICE:
xlwt對Excel只能進行只寫操作
xrrd對Excel只能進行只讀操作
此外,還有xlutils.copy可以實現Excel的復制再編輯。
1.python寫excel — xlwt常用功能
A.準備工作
安裝xlwt :在終端中輸入pip install xlwt或者easy_install xlwt
引入xlwt包 :    
import xlwt # 寫
B.基礎教程
新建工作簿&增加sheet: 新建一個工作簿,然后往里添加sheet    
f = xlwt.Workbook() # 創建工作簿
sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True)
#一個excel表格中可以添加多個sheet
往sheet中寫入內容: sheet.write函數可以傳三個參數
第i(參數1)第j(參數2)列存入內容(參數3)    
sheet1.write(i, j, '第i行第j列存放此內容', style)
# 這條語句實現的功能就是往第i行第j列存第三個參數的內容,第四個參數是樣式(如字體,背景),可以不傳第四個參數。
合并單元格并寫入內容:    
sheet1.write_merge(x, x + m, y, y + n, '內容', style)
# 這條y語句表示將[x:x+m]行[y:y+n]列的矩陣合并成一個單元格。存放第五個參數的內容,同理,style參數可以不傳參

最后使用f.save(‘demo')
就可以把f保存到excel了
C.實戰

我們可以先新建一個工作簿,然后往里添加兩個sheet,然后查看效果


#coding=utf-8
importxlwt
 
f=xlwt.Workbook()# 創建工作簿
sheet1=f.add_sheet(u'葡小萄', cell_overwrite_ok=True)
sheet2=f.add_sheet(u'小葡萄', cell_overwrite_ok=True)
 
f.save('xlwt_tutorial')



效果如下,發現表格xlwt_tutorial中有兩個sheet。

我們開始往sheet中寫入內容,不傳入style參數

先只使用write函數



#coding=utf-8
importxlwt
 
f=xlwt.Workbook()# 創建工作簿
sheet1=f.add_sheet(u'葡小萄', cell_overwrite_ok=True)
sheet2=f.add_sheet(u'小葡萄', cell_overwrite_ok=True)
 
row=0
temp=[u'姓名',u'年齡',u'學校',u'專業']
forpos,vinenumerate(temp):
 sheet1.write(row,pos,v)
row+=1
sheet1.write(row,0,u'葡萄')
sheet1.write(row,1,18)
sheet1.write(row,2,u'北京電影學院')
row+=1
sheet1.write(row,0,u'椰子')
sheet1.write(row,1,20)
sheet1.write(row,2,u'帝國國王科技大學')
 
f.save('xlwt_tutorial')



效果如下,我們建立了一個3行4列的表格。(write函數行和列值都是從0開始的)

下面我們使用write_merge函數來合并單元格并寫入

在f.save之前添加一行代碼



sheet1.write_merge(1,2,3,3,u'漢語言文學')


效果如下,將第2-3行第4列合并


2.pythonxd讀excel —xlrd常用功能
A.準備工作
安裝xlrd :在終端中輸入pip install xlrd或者easy_install xlrd
引入xlrd包 :    
import xlrd # 讀

B.基礎教程&實戰

打開一個Excel,然后輸出所有sheet的名字    
#coding=utf-8
 
import xlrd
import uniout
 
f = xlrd.open_workbook(r'xlwt_tutorial')
print f.sheet_names()

輸出:[u'葡小萄', u'小葡萄']

得到表格里的所有的sheet    
for i in range(len(f.sheet_names())):
  sheet1 = workbook.sheet_by_index(i)

得到sheet中的內容    
f = xlrd.open_workbook(r'xlwt_tutorial')
sheet1 = f.sheet_by_index(0) #打開第一個sheet
sheet2 = f.sheet_by_name(u'小葡萄') #打開名字為小葡萄的sheet
 
#輸出sheet的名稱,行數,列數
print sheet1.name,sheet1.nrows,sheet1.ncols
print sheet2.name,sheet2.nrows,sheet2.ncols

輸出為:
葡小萄 3 4
小葡萄 0 0
.    
print sheet1.row_values(1) #獲取第二行內容
print sheet1.col_values(2) #獲取第三列內容

輸出為:
[u'葡萄', 18.0, u'北京電影學院', u'漢語言文學']
[u'學校', u'北京電影學院', u'帝國國王科技大學']
.


# 獲取單元格內容
printsheet1.cell(1,0).value
 
# 獲取單元格內容的數據類型
printsheet1.cell(1,1).ctype
 
#ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error



輸出為:
葡萄
2

3.xlutils 常用功能

A.準備工作

安裝xlutils :在終端中輸入pip install xlutils或者easy_install xlutils
引入xlutils包 :



importxlutils



B.xlutils中copy功能

我們可能會遇到一個問題,想對一個存儲好的Excel進行編輯。
但是xlrd是只讀模式,不能進行編寫。
而xlwt是只寫模式,不能讀入Excel文件進行編輯。
我們可以采用xlrd打開一個文檔,后采用xlutils中copy功能把文檔拷貝,然后進行編輯即可。



importxlrd
fromxlutils.copyimportcopy
 
f=xlrd.open_workbook(r'xlwt_tutorial')
wb=copy(f)# 將f拷貝到wb
 
sheet1=wb.get_sheet(0)# 打開sheet
printsheet1.name
sheet1.write(3,0,'change')
 
wb.save('xlwt_tutorial')


輸出為:
葡小萄
輸出的表格已經改變。

PS: 可以看到第二行第四列和第三行第四列合并格已經在COPY的時候被毀掉了。

4.xlwt寫Excel時公式的應用

我們寫用xlwt寫一個表格



#coding=utf-8
importxlwt
 
f=xlwt.Workbook()# 創建工作簿
sheet1=f.add_sheet(u'得分統計', cell_overwrite_ok=True)
 
mdict={"monkey":{"writing":80,"reading":60,"speaking":70,"listening":60},
"grape":{"writing":100,"reading":80,"speaking":70,"listening":60}}
 
sheet1.write(0,0,u'得分統計')
sheet1.write(1,0,u'書法得分')
sheet1.write(2,0,u'閱讀得分')
sheet1.write(3,0,u'演講得分')
sheet1.write(4,0,u'聽力得分')
temp=['writing','reading','speaking','listening']
 
forpos,nameinenumerate(mdict):
 sheet1.write(0,pos+1,name)
 forp,vinenumerate(temp):
  sheet1.write(p+1,pos+1,mdict[name][v])
 
f.save('得分統計')



打開表格為:

我們現在想做的是統計grape的總分和monkey的總分:
在f.save之前加入代碼:


sheet1.write(5,0,u'總分統計')

foriinrange(len(mdict)):
 forstr=chr(65+i+1)+'2+'+chr(65+i+1)+'3+'+chr(65+i+1)+'4+'+chr(65+i+1)+'5'
 printforstr
 sheet1.write(5,i+1,xlwt.Formula(forstr))

輸出為:
B2+B3+B4+B5
C2+C3+C4+C5
打開表格為:


5.xlwt寫入特定目錄(路徑設置)

由于代碼分層的緣故,使代碼整體框架優美。
我們需要把文件寫入到特定目錄下。
但是由于xlwt中沒有直接寫入到特定目錄的函數。
因此使用shutil.move函數來把文件MOV到特定目錄下:

#coding=utf-8
importxlwt
importos
importshutil
 
path='../sheet/'
isExists=os.path.exists(path)# 判斷目錄是否存在
 
ifnotisExists:# 如果目錄不存在,新建目錄
 os.makedirs(path)
 
f=xlwt.Workbook()# 創建工作簿
sheet1=f.add_sheet(u'得分統計', cell_overwrite_ok=True)
 
mdict={"monkey":{"writing":80,"reading":60,"speaking":70,"listening":60},
"grape":{"writing":100,"reading":80,"speaking":70,"listening":60}}
 
sheet1.write(0,0,u'得分統計')
sheet1.write(1,0,u'書法得分')
sheet1.write(2,0,u'閱讀得分')
sheet1.write(3,0,u'演講得分')
sheet1.write(4,0,u'聽力得分')
temp=['writing','reading','speaking','listening']
 
forpos,nameinenumerate(mdict):
 sheet1.write(0,pos+1,name)
 forp,vinenumerate(temp):
  sheet1.write(p+1,pos+1,mdict[name][v])
 
sheet1.write(5,0,u'總分統計')
foriinrange(len(mdict)):
 forstr=chr(65+i+1)+'2+'+chr(65+i+1)+'3+'+chr(65+i+1)+'4+'+chr(65+i+1)+'5'
 printforstr
 sheet1.write(5,i+1,xlwt.Formula(forstr))
 
f.save('得分統計')
shutil.move(u'得分統計', path)

效果圖:

總結

以上就是本文關于python實現報表自動化詳解的全部內容,希望對大家有所幫助

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢