Python?Excel數(shù)據(jù)處理之xlrd/xlwt/xlutils模塊詳解
常規(guī)的Excel數(shù)據(jù)處理中,就是對Excel數(shù)據(jù)文件的讀/寫/文件對象操作。
通過對應(yīng)的python非標(biāo)準(zhǔn)庫xlrd/xlwt/xlutils,來實現(xiàn)具體的數(shù)據(jù)處理業(yè)務(wù)邏輯。
在復(fù)雜的Excel業(yè)務(wù)數(shù)據(jù)處理中,三兄弟扮演的角色缺一不可。如何能夠使用xlrd/xlwt/xlutils三個模塊來實現(xiàn)數(shù)據(jù)處理就是今天的內(nèi)容。
1、模塊說明
使用該三個模塊來處理Excel數(shù)據(jù)最好的地方就是他們和Excel文件對象對應(yīng)的數(shù)據(jù)處理概念是一樣的,能更好的便于我們理解數(shù)據(jù)對象。
首先,這三個模塊都是python的非標(biāo)準(zhǔn)庫,可以選擇pip的方式來進(jìn)行安裝。
pip?install?xlrd pip?install?xlwt pip?install?xlutils
下面是我們?yōu)檠菔緮?shù)據(jù)處理的過程準(zhǔn)備的源數(shù)據(jù)內(nèi)容,只是用于測試。

xlrd:用于讀取Excle數(shù)據(jù)文件將返回的數(shù)據(jù)對象放到內(nèi)存中,然后查詢數(shù)據(jù)文件對象的相關(guān)信息。
xlwt:用于在內(nèi)存中生成新的數(shù)據(jù)文件對象,處理完成后寫入到Excel數(shù)據(jù)文件中。
xlutils:主要的作用就是copy新的文件對象,在新的數(shù)據(jù)對象中完成數(shù)據(jù)處理操作。
將xlrd/xlwt/xlutils三個模塊分別都導(dǎo)入到待開發(fā)的代碼塊中提供支持。
#?Importing?the?xlrd?module. import?xlrd?as?read #?Importing?the?xlwt?module. import?xlwt?as?write #?Copying?the?contents?of?the?original?workbook?into?a?new?workbook. from?xlutils.copy?import?copy
2、xlrd處理
#?Opening?the?workbook?and?assigning?it?to?the?variable?`work_book`.
work_book?=?read.open_workbook('D:/test-data-work/test.xls')
#?Assigning?the?sheet?named?'Sheet1'?to?the?variable?`sheet`.
sheet?=?work_book.sheet_by_name('Sheet1')
#?`row?=?sheet.nrows`?is?assigning?the?number?of?rows?in?the?sheet?to?the?variable?`row`.
row?=?sheet.nrows
#?`col?=?sheet.ncols`?is?assigning?the?number?of?columns?in?the?sheet?to?the?variable?`col`.
col?=?sheet.ncols
print('Sheet1工作表有:{0}行,{1}列'.format(str(row),?str(col)))
# Sheet1工作表有:23行,5列
下面是三種常用的sheet對象的數(shù)據(jù)遍歷方式,分別是按行/列的方式進(jìn)行數(shù)據(jù)遍歷。
for?a?in?sheet.get_rows(): ????print(a) #?[text:'姓名',?text:'年齡',?text:'班級',?text:'成績',?text:'表現(xiàn)'] #?[text:'Python?集中營',?number:20.0,?number:1210.0,?number:90.0,?text:'A'] #?[text:'Python?集中營',?number:21.0,?number:1211.0,?number:91.0,?text:'A'] #?[text:'Python?集中營',?number:22.0,?number:1212.0,?number:92.0,?text:'A'] #?[text:'Python?集中營',?number:23.0,?number:1213.0,?number:93.0,?text:'A'] #?[text:'Python?集中營',?number:24.0,?number:1214.0,?number:94.0,?text:'A'] #?[text:'Python?集中營',?number:25.0,?number:1215.0,?number:95.0,?text:'A'] #?[text:'Python?集中營',?number:26.0,?number:1216.0,?number:96.0,?text:'A'] #?[text:'Python?集中營',?number:27.0,?number:1217.0,?number:97.0,?text:'A'] #?[text:'Python?集中營',?number:28.0,?number:1218.0,?number:98.0,?text:'A'] #?[text:'Python?集中營',?number:29.0,?number:1219.0,?number:99.0,?text:'A'] #?[text:'Python?集中營',?number:30.0,?number:1220.0,?number:100.0,?text:'A'] #?[text:'Python?集中營',?number:31.0,?number:1221.0,?number:101.0,?text:'A'] #?[text:'Python?集中營',?number:32.0,?number:1222.0,?number:102.0,?text:'A'] #?[text:'Python?集中營',?number:33.0,?number:1223.0,?number:103.0,?text:'A'] #?[text:'Python?集中營',?number:34.0,?number:1224.0,?number:104.0,?text:'A'] #?[text:'Python?集中營',?number:35.0,?number:1225.0,?number:105.0,?text:'A'] #?[text:'Python?集中營',?number:36.0,?number:1226.0,?number:106.0,?text:'A'] #?[text:'Python?集中營',?number:37.0,?number:1227.0,?number:107.0,?text:'A'] #?[text:'Python?集中營',?number:38.0,?number:1228.0,?number:108.0,?text:'A'] #?[text:'Python?集中營',?number:39.0,?number:1229.0,?number:109.0,?text:'A'] #?[text:'Python?集中營',?number:40.0,?number:1230.0,?number:110.0,?text:'A'] #?[text:'Python?集中營',?number:41.0,?number:1231.0,?number:111.0,?text:'A'] for?b?in?range(row): ????print(sheet.row_values(b)) #?['姓名',?'年齡',?'班級',?'成績',?'表現(xiàn)'] #?['Python?集中營',?20.0,?1210.0,?90.0,?'A'] #?['Python?集中營',?21.0,?1211.0,?91.0,?'A'] #?['Python?集中營',?22.0,?1212.0,?92.0,?'A'] #?['Python?集中營',?23.0,?1213.0,?93.0,?'A'] #?['Python?集中營',?24.0,?1214.0,?94.0,?'A'] #?['Python?集中營',?25.0,?1215.0,?95.0,?'A'] #?['Python?集中營',?26.0,?1216.0,?96.0,?'A'] #?['Python?集中營',?27.0,?1217.0,?97.0,?'A'] #?['Python?集中營',?28.0,?1218.0,?98.0,?'A'] #?['Python?集中營',?29.0,?1219.0,?99.0,?'A'] #?['Python?集中營',?30.0,?1220.0,?100.0,?'A'] #?['Python?集中營',?31.0,?1221.0,?101.0,?'A'] #?['Python?集中營',?32.0,?1222.0,?102.0,?'A'] #?['Python?集中營',?33.0,?1223.0,?103.0,?'A'] #?['Python?集中營',?34.0,?1224.0,?104.0,?'A'] #?['Python?集中營',?35.0,?1225.0,?105.0,?'A'] #?['Python?集中營',?36.0,?1226.0,?106.0,?'A'] #?['Python?集中營',?37.0,?1227.0,?107.0,?'A'] #?['Python?集中營',?38.0,?1228.0,?108.0,?'A'] #?['Python?集中營',?39.0,?1229.0,?109.0,?'A'] #?['Python?集中營',?40.0,?1230.0,?110.0,?'A'] #?['Python?集中營',?41.0,?1231.0,?111.0,?'A'] for?c?in?range(col): ????print(sheet.col_values(c)) #?['姓名',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營',?'Python?集中營'] #?['年齡',?20.0,?21.0,?22.0,?23.0,?24.0,?25.0,?26.0,?27.0,?28.0,?29.0,?30.0,?31.0,?32.0,?33.0,?34.0,?35.0,?36.0,?37.0,?38.0,?39.0,?40.0,?41.0] #?['班級',?1210.0,?1211.0,?1212.0,?1213.0,?1214.0,?1215.0,?1216.0,?1217.0,?1218.0,?1219.0,?1220.0,?1221.0,?1222.0,?1223.0,?1224.0,?1225.0,?1226.0,?1227.0,?1228.0,?1229.0,?1230.0,?1231.0] #?['成績',?90.0,?91.0,?92.0,?93.0,?94.0,?95.0,?96.0,?97.0,?98.0,?99.0,?100.0,?101.0,?102.0,?103.0,?104.0,?105.0,?106.0,?107.0,?108.0,?109.0,?110.0,?111.0] #?['表現(xiàn)',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A',?'A']
3、xlwt處理
#?Creating?a?new?workbook.
work_book_2?=?write.Workbook()
#?Creating?a?new?sheet?named?'Sheet4'?in?the?workbook.
sheet_2?=?work_book_2.add_sheet('Sheet4')
list?=?[
????['姓名',?'年齡',?'班級',?'成績'],
????['張三',?'20',?'1210',?'89'],
????['李四',?'21',?'1211',?'90'],
????['王五',?'22',?'1212',?'91'],
]
for?row_index?in?range(4):
????for?col_index?in?range(4):
????????sheet_2.write(row_index,?col_index,?list[row_index][col_index])
????????col_index?+=?1
????row_index?+=?1
#?Saving?the?workbook?to?the?specified?location.
work_book_2.save('D:/test-data-work/test2.xls')
4、xlutils處理
#?Opening?the?workbook?and?assigning?it?to?the?variable?`work_book_3`.
work_book_3?=?read.open_workbook('D:/test-data-work/test.xls')
#?Copying?the?contents?of?the?original?workbook?into?a?new?workbook.
work_book_3_copy?=?copy(work_book_3)
#?Saving?the?contents?of?the?original?workbook?into?a?new?workbook.
work_book_3_copy.save('D:/test-data-work/test3.xls')到此這篇關(guān)于Python Excel數(shù)據(jù)處理之xlrd/xlwt/xlutils模塊詳解的文章就介紹到這了,更多相關(guān)Python Excel數(shù)據(jù)處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用python matploblib庫繪制準(zhǔn)確率,損失率折線圖
這篇文章主要介紹了使用python matploblib庫繪制準(zhǔn)確率,損失率折線圖,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
利用Python實現(xiàn)Shp格式向GeoJSON的轉(zhuǎn)換方法
JSON(JavaScript Object Nonation)是利用鍵值對+嵌套來表示數(shù)據(jù)的一種格式,以其輕量、易解析的優(yōu)點,這篇文章主要介紹了利用Python實現(xiàn)Shp格式向GeoJSON的轉(zhuǎn)換,需要的朋友可以參考下2019-07-07
對Python3.x版本print函數(shù)左右對齊詳解
今天小編就為大家分享一篇對Python3.x版本print函數(shù)左右對齊詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
使用python實現(xiàn)遞歸版漢諾塔示例(漢諾塔遞歸算法)
這篇文章主要介紹了使用python實現(xiàn)遞歸版漢諾塔示例(漢諾塔遞歸算法),需要的朋友可以參考下2014-04-04
只需要100行Python代碼就可以實現(xiàn)的貪吃蛇小游戲
貪吃蛇小游戲相信80、90后小時候肯定都玩過,那么你知道如果通過Python來實現(xiàn)嗎?今天就來教大家,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下2021-05-05
使用Python通過win32 COM打開Excel并添加Sheet的方法
今天小編就為大家分享一篇使用Python通過win32 COM打開Excel并添加Sheet的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Pandas實現(xiàn)數(shù)據(jù)類型轉(zhuǎn)換的一些小技巧匯總
這篇文章主要給大家匯總介紹了關(guān)于Pandas實現(xiàn)數(shù)據(jù)類型轉(zhuǎn)換的一些小技巧,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05

