
寫在前面
Python常見的數據文件處理有5種,今天我們來講講其中的Excel如何處理!
Excel是大家工作當中使用頻率比較高的一款辦公軟件了所以我們很有必要學習一下,那么Python是如何處理excel呢,下面就來講講~~
正文

1.兩大庫xlrd,xlwt
1).Python操作excel主要用到xlrd和xlwt這兩個庫
即xlrd是讀excel,xlwt是寫excel的庫,名字也蠻好記得,xl是excel的縮寫,rd是read,wt是write.xlrd可以解析微軟的.xls and .xlsx兩種各種的電子表格
2).如何安裝
用pip install xlrd就可以安裝xlrd模塊
用pip install xlwt就可以安裝xlwt模塊
如果小伙伴是用Pycharm的話更簡單,直接打開File/Setting/Project/Project Interpreter,然后選擇左邊的綠色加號安裝
2.如何讀一個excel文件
比如有這樣一個"user_data.xlsx"表格,第一個sheet叫"data",內容如下:

1).打開表格
file_name='user_data.xlsx'
excel_file=os.getcwd()+'\'+file_name
rdata=xlrd.open_workbook(excel_file)
print type(rdata)
>>
我們用open_workbook這個函數打開一個excel文件,並返回一個rdata對象,有同學好奇這個rdata是啥,我們type一下
發現data是:xlrd這個模塊下面的book文件下面的Book類的實例對象
有點拗口,但確實是這樣的,不信可以看源碼
2).獲取表格的基本信息
print 'sheets nums:',rdata.nsheets#excel sheets 個數
>>
sheets nums: 1
3).每個sheets名字
print 'sheets names:',rdata.sheet_names()#excel sheets 每個名字
>>
sheets names: [u'data']
4).每個sheet的行列總數,比如第一個sheet
sheet1=rdata.sheet_by_index(0)
print 'rows:',sheet1.nrows
print 'clos',sheet1.ncols
>>rows=11,cols=3
5),獲取行,列的對象
獲取第一行的內容
sh1=rdata.sheet_by_index(0)
print sh1.row(0)
>>
[text:u'\u65f6\u95f4', text:u'\u4eba\u6570']
print sh1.row_values(1)
>>
[u'\u65f6\u95f4', u'\u4eba\u6570']
#返回的是列表對象,中文會轉成的unicode顯示
獲取第二列的內容
print sh1.col(1)
>>
[text:u'\u4eba\u6570', number:16.0, number:21.0,
number:34.0, number:48.0, number:30.0, number:36.0,
number:28.0, number:26.0, number:24.0, number:46.0]
#返回的是列表對象,text表示是文本對象,number是數字
>>print sh1.col_values(1)
[u'\u4eba\u6570', 16.0, 21.0, 34.0, 48.0, 30.0,
36.0, 28.0, 26.0, 24.0, 46.0]
我們可以利用列表切片訪問:第二列到第5列
>>print sh1.col_values(1)[1:5]
[16.0, 21.0, 34.0, 48.0]
也可以利用默認的col_values參數
col_values(self, colx, start_rowx=0, end_rowx=None)
print sh1.col_values(1,1,5)
>>
[16.0, 21.0, 34.0, 48.0]
6).獲取單元格cell的內容
xlrd對excel里面內容分成下面7種的,是枚舉類型
(
XL_CELL_EMPTY,
XL_CELL_TEXT,
XL_CELL_NUMBER,
XL_CELL_DATE,
XL_CELL_BOOLEAN,
XL_CELL_ERROR,
XL_CELL_BLANK, # for use in debugging, gathering stats, etc
) = range(7)
我們來看一下,第一行第一列的單元格是個字符串
sh1=rdata.sheet_by_index(0)
cell_0_0=sh1.cell(0,0)
print cell_0_0
print cell_0_0.ctype
print cell_0_0.value
>>
text:u'\u65f6\u95f4'
1
時間
#1確實對應的是文本
我們來看一下,第二行第一列的單元格:日期
cell_1_0=sh1.cell(1,0)
print cell_1_0
print cell_1_0.ctype
print cell_1_0.value
>>
xldate:42736.0
3
42736.0
#3確實對應的是日期 (有小伙伴問日期怎么變成這個數字,
#因為日期被轉換成了xldate對象,一會我們會轉換回來,后面會詳細講)
我們來看一下,第二行第二列的單元格:數字
cell_1_1=sh1.cell(1,1)
print cell_1_1
print cell_1_1.ctype
>>
number:16.0
2
16.0
#2確實對應的是日期 (有小伙伴問日期怎么變成這個數字,不急后面會講)
3.如何寫數據進表格
主要是用xlwt模塊,現在我們要把上面的'data.xlsx'表格中人數1和人數2相加等於總數列,並寫入到一個新的excel文件中去.
1).讀取'data.xlsx'中sheet1的數據
import xlrd
import os
file_name='data.xlsx'
excel_file=os.getcwd()+'\'+file_name
rdata=xlrd.open_workbook(excel_file)
sh1=rdata.sheet_by_index(0)
2).創建一個wbook對象,生成一個新的sheet
import xlwt
wbook=xlwt.Workbook()
wsheet=wbook.add_sheet(sh1.name)
3).在寫入第一行,標題欄
wsheet這個函數(row,col,value,style),這個style其實就是這個內容單元格的格式
style=xlwt.easyxf('align: vertical center, horizontal center')
wsheet.write(0,0,u'時間',style)
wsheet.write(0,1,u'人數1',style)
wsheet.write(0,2,u'人數2',style)
wsheet.write(0,3,u'總分',style)
4).寫入時間列的數據,需要轉化數據格式
上面讀表格的時候,我們遺留了一個問題,就是第一列的日期
為啥打印出來,會變成奇怪的數據
其實那個是xldate對象,我們需要把sheet1里面的內容提取處理,然后轉成日期數

5).計算第二列和第三列的數據,得到總分

6).把sheet1里面的第二列,第三列和總分的數據寫入excel文件

7).寫成文件new_data.xls
try:
wbook.save('new_data.xls')
except Exception as e:
print e
else:
print 'write excel file successful
運行一下,打開新的表格new_data.xls

寫在后面
不知道大家看了文章會不會操作了呢?