xlwt 常用功能
xlrd 常用功能
xlutils 常用功能
xlwt寫Excel時公式的應用
xlwt寫入特定目錄(路徑設置)
xlwt?Python語言中,寫入Excel文件的擴展工具。可以實現指定表單、指定單元格的寫入。支持excel03版到excel2013版。使用時請確保已經安裝python環境。百度百科
xlrd Python語言中,讀取Excel的擴展工具。可以實現指定表單、指定單元格的讀取。使用時請確保已經安裝python環境。百度百科
NOTICE:
xlwt對Excel只能進行只寫操作
xrrd對Excel只能進行只讀操作
此外,還有xlutils.copy可以實現Excel的復制再編輯。
1.python寫excel — xlwt常用功能
A.準備工作
安裝xlwt?:在終端中輸入pip install xlwt或者easy_install xlwt
引入xlwt包?:
#?寫11
B.基礎教程
新建工作簿&增加sheet: 新建一個工作簿,然后往里添加sheet
f?=?xlwt.Workbook()??#?創建工作簿 sheet1?=?f.add_sheet(u'sheet1',?cell_overwrite_ok=True)#一個excel表格中可以添加多個sheet123123
往sheet中寫入內容: sheet.write函數可以傳三個參數
第i(參數1)第j(參數2)列存入內容(參數3)
。1212
合并單元格并寫入內容:
sheet1.write_merge(x, x + m, y, y + n, '內容', style)# 這條y語句表示將[x:x+m]行[y:y+n]列的矩陣合并成一個單元格。存放第五個參數的內容,同理,style參數可以不傳參1212
最后使用f.save(‘demo’)
就可以把f保存到excel了
C.實戰
我們可以先新建一個工作簿,然后往里添加兩個sheet,然后查看效果
#coding=utf-8import?xlwt 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')1234567812345678
效果如下,發現表格xlwt_tutorial中有兩個sheet。
我們開始往sheet中寫入內容,不傳入style參數
先只使用write函數
#coding=utf-8import?xlwt f?=?xlwt.Workbook()??#?創建工作簿sheet1?=?f.add_sheet(u'葡小萄',?cell_overwrite_ok=True) sheet2?=?f.add_sheet(u'小葡萄',?cell_overwrite_ok=True) row?=?0temp?=?[u'姓名',u'年齡',u'學校',u'專業'] for?pos,v?in?enumerate(temp): ????sheet1.write(row,pos,v) row?+=?1sheet1.write(row,0,u'葡萄') sheet1.write(row,1,18) sheet1.write(row,2,u'北京電影學院') row?+=?1sheet1.write(row,0,u'椰子') sheet1.write(row,1,20) sheet1.write(row,2,u'帝國國王科技大學') f.save('xlwt_tutorial')123456789101112131415161718192021123456789101112131415161718192021
效果如下,我們建立了一個3行4列的表格。(write函數行和列值都是從0開始的)
下面我們使用write_merge函數來合并單元格并寫入
在f.save之前添加一行代碼
sheet1.write_merge(1,2,3,3,u'漢語言文學')11
效果如下,將第2-3行第4列合并
2.pythonxd讀excel —xlrd常用功能
A.準備工作
安裝xlrd?:在終端中輸入pip install xlrd或者easy_install xlrd
引入xlrd包?:
import?xlrd??#?讀11
B.基礎教程&實戰
打開一個Excel,然后輸出所有sheet的名字
#coding=utf-8import?xlrdimport?uniout f?=?xlrd.open_workbook(r'xlwt_tutorial')print?f.sheet_names()12345671234567
輸出:[u’葡小萄’, u’小葡萄’]
得到表格里的所有的sheet
for?i?in?range(len(f.sheet_names())): ?????sheet1?=?workbook.sheet_by_index(i)1212
得到sheet中的內容
f?=?xlrd.open_workbook(r'xlwt_tutorial') sheet1?=?f.sheet_by_index(0)??#打開第一個sheetsheet2?=?f.sheet_by_name(u'小葡萄')??#打開名字為小葡萄的sheet#輸出sheet的名稱,行數,列數print?sheet1.name,sheet1.nrows,sheet1.ncolsprint?sheet2.name,sheet2.nrows,sheet2.ncols12345671234567
輸出為:
葡小萄 3 4
小葡萄 0 0
.
print?sheet1.row_values(1)??#獲取第二行內容print?sheet1.col_values(2)??#獲取第三列內容1212
輸出為:
[u’葡萄’, 18.0, u’北京電影學院’, u’漢語言文學’]
[u’學校’, u’北京電影學院’, u’帝國國王科技大學’]
.
#?獲取單元格內容print?sheet1.cell(1,0).value#?獲取單元格內容的數據類型print?sheet1.cell(1,1).ctype#ctype?:?0?empty,1?string,?2?number,?3?date,?4?boolean,?5?error12345671234567
輸出為:
葡萄
2
3.xlutils 常用功能
A.準備工作
安裝xlutils?:在終端中輸入pip install xlutils或者easy_install xlutils
引入xlutils包?:
import?xlutils??11
B.xlutils中copy功能
我們可能會遇到一個問題,想對一個存儲好的Excel進行編輯。
但是xlrd是只讀模式,不能進行編寫。
而xlwt是只寫模式,不能讀入Excel文件進行編輯。
我們可以采用xlrd打開一個文檔,后采用xlutils中copy功能把文檔拷貝,然后進行編輯即可。
import?xlrdfrom?xlutils.copy?import?copy f?=?xlrd.open_workbook(r'xlwt_tutorial') wb?=?copy(f)?#?將f拷貝到wbsheet1?=?wb.get_sheet(0)?#?打開sheetprint?sheet1.name sheet1.write(3,0,'change') wb.save('xlwt_tutorial')12345678910111234567891011
輸出為:
葡小萄
輸出的表格已經改變。
PS: 可以看到第二行第四列和第三行第四列合并格已經在COPY的時候被毀掉了。
4.xlwt寫Excel時公式的應用
我們寫用xlwt寫一個表格
#coding=utf-8import?xlwt 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']for?pos,name?in?enumerate(mdict): ????sheet1.write(0,pos+1,name)????for?p,v?in?enumerate(temp): ????????sheet1.write(p+1,pos+1,mdict[name][v]) f.save('得分統計')1234567891011121314151617181920212212345678910111213141516171819202122
打開表格為:
我們現在想做的是統計grape的總分和monkey的總分:
在f.save之前加入代碼:
sheet1.write(5,0,u'總分統計')for?i?in?range(len(mdict)): ????forstr?=?chr(65+i+1)+'2+'+chr(65+i+1)+'3+'+chr(65+i+1)+'4+'+chr(65+i+1)+'5' ????print?forstr ????sheet1.write(5,i+1,xlwt.Formula(forstr))1234512345
輸出為:
B2+B3+B4+B5
C2+C3+C4+C5
打開表格為:
5.xlwt寫入特定目錄(路徑設置)
由于代碼分層的緣故,使代碼整體框架優美。
我們需要把文件寫入到特定目錄下。
但是由于xlwt中沒有直接寫入到特定目錄的函數。
因此使用shutil.move函數來把文件MOV到特定目錄下:
#coding=utf-8import?xlwtimport?osimport?shutil path?=?'../sheet/'isExists?=?os.path.exists(path)?#?判斷目錄是否存在if?not?isExists:???#?如果目錄不存在,新建目錄 ????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']for?pos,name?in?enumerate(mdict): ????sheet1.write(0,pos+1,name)????for?p,v?in?enumerate(temp): ????????sheet1.write(p+1,pos+1,mdict[name][v]) sheet1.write(5,0,u'總分統計')for?i?in?range(len(mdict)): ????forstr?=?chr(65+i+1)+'2+'+chr(65+i+1)+'3+'+chr(65+i+1)+'4+'+chr(65+i+1)+'5' ????print?forstr ????sheet1.write(5,i+1,xlwt.Formula(forstr)) f.save('得分統計') shutil.move(u'得分統計',?path)1234567891011121314151617181920212223242526272829303132333435363712345678910111213141516171819202122232425262728293031323334353637
效果圖: