使用Python的xlrd模塊讀取Excel表格
在日常工作中,我們需要從Excel表格中提取數(shù)據(jù)進(jìn)行分析。對于Python編程者而言,如何使用Python讀取Excel表格數(shù)據(jù)是一個必須掌握的技能。本文介紹了使用Python的xlrd模塊來操作Ex
在日常工作中,我們需要從Excel表格中提取數(shù)據(jù)進(jìn)行分析。對于Python編程者而言,如何使用Python讀取Excel表格數(shù)據(jù)是一個必須掌握的技能。本文介紹了使用Python的xlrd模塊來操作Excel表格,以及封裝了一個OpExcel類來實現(xiàn)各種功能。
導(dǎo)入模塊
在使用Python讀取Excel表格之前,我們需要導(dǎo)入xlrd模塊,如果系統(tǒng)默認(rèn)編碼不是utf-8,還需要重新設(shè)置一下編碼格式。
```
import xlrd
import sys
reload(sys)
('utf-8')
```
封裝OpExcel類
為方便操作Excel表格,我們封裝了一個OpExcel類,用于處理Excel表格。首先在類的初始化函數(shù)中打開Excel文件,并獲取所有sheet名稱。
```
class OpExcel(object):
"""操作Excel"""
def __init__(self, file_path):
super(OpExcel, self).__init__()
_path file_path
self.Excel _workbook(file_path)
_names()
```
獲取sheet索引
由于每個Excel文件可能包含多個sheet,我們需要根據(jù)sheet名稱來獲取要操作的sheet的索引值。為此,我們添加了get_tableindex方法,用于獲取sheet的索引值。
```
def get_tableindex(self, table):
"""獲取excel需要操作的sheet的索引值"""
if table in
index_data(table)
return (index_data)
else:
print (table u'不是excel的sheet名稱')
return False
```
獲取所有數(shù)據(jù)
為了方便操作,我們可以將Excel表格中的所有數(shù)據(jù)按照字典形式保存,并按行取Excel表格中的數(shù)據(jù)。為此,我們添加了get_data方法。
```
def get_data(self):
"""獲取excel所有數(shù)據(jù)已字典形式保存,按行取excle"""
excle{}
for table in
all_value[]
_by_name(table)
for i in range():
all_(_values(i))
excle[table]all_value
return excle
```
獲取表行數(shù)和列數(shù)
有時候我們需要知道Excel表格中某個sheet的行數(shù)或列數(shù),為此,我們添加了get_nrows和get_mcols方法分別用于獲取表的行數(shù)和列數(shù)。
```
def get_nrows(self, table):
"""獲取表行數(shù)"""
_by_name(table)
return
def get_mcols(self, table):
"""獲取表列數(shù)"""
_by_name(table)
return
```
獲取某一行數(shù)據(jù)和某一列數(shù)據(jù)
有時候我們只需要獲取Excel表格中某一行或某一列的數(shù)據(jù),為此,我們添加了get_rowdate和get_coldate方法。
```
def get_rowdate(self, table, nrow0, mcol0):
"""從M列開始獲取第N行數(shù)據(jù)"""
_by_name(table)
data_values(nrow)
return (data[mcol:])
def get_coldate(self, table, nrow0, mcol0):
"""從N行開始獲取第M列數(shù)據(jù)"""
_by_name(table)
data_values(mcol)
return (data[nrow:])
```
獲取某個單元格的數(shù)據(jù)和類型
有時候我們需要獲取Excel表格中某個單元格的數(shù)據(jù)和數(shù)據(jù)類型,為此,我們添加了get_cellate方法。
```
def get_cellate(self, table, nrow0, mcol0):
"""獲取某個單元格數(shù)據(jù)和類型
ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error"""
_by_name(table)
type(nrow,mcol).ctype
if (type 3):
data_value xlrd.xldate_as_tuple((nrow,mcol).value,)
value datetime(*data_value).strftime('%Y/%m/%d %H:%M:%S')
else:
value(nrow,mcol).value
data[type,value]
return (data)
```
獲取合并單元格的行和列
有時候我們需要獲取Excel表格中合并單元格有值的行和列,為此,我們添加了get_mergedcell方法。
```
def get_mergedcell(self, table):
"""獲取合并單元格有值得行和列"""
_by_name(table)
data []
for (nrow,rowhigh,mcol,colhigh) in _cells:
([nrow,mcol])
return (data)
```
檢查Excel表格中特定數(shù)據(jù)類型的單元格
有時候我們需要檢查Excel表格中是否存在特定的單元格數(shù)據(jù)類型,為此,我們添加了get_ctype3方法。
```
def get_ctype3(self, table):
"""檢查每一個單元格的ctype:0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
輸出ctype3的單元格"""
nrows_nrows(table)
mcols_mcols(table)
data[]
for nrow in range(nrows):
for mcol in range(mcols):
type(nrow,mcol).ctype
if (type3):
([nrow,mcol])
if data![]:
return data
else:
return False
```
根據(jù)內(nèi)容查找某一行的數(shù)據(jù)
有時候我們需要根據(jù)內(nèi)容查找Excel表格中某一行的數(shù)據(jù),為此,我們添加了get_datarow方法。
```
def get_datarow(self, table, data, cols0):
"""根據(jù)內(nèi)容查找nrow行"""
if type(data)str or type(data)unicode:
mcolNone
nlist[]
datanrow{}
table_data_coldate(table,0,cols)
print(u'開始查找數(shù)據(jù)')
for nrow,row_data in enumerate(table_data):
if row_data data:
(nrow)
print(u'%s is in the %s Row'%(data,str(nrow)))
if nlist![] :
datanrow[data]nlist
return (datanrow)
else:
print(u'沒匹配到數(shù)據(jù)')
return False
else:
print(u'Value type is wrong')
return False
```
總結(jié):
本文介紹了使用Python的xlrd模塊來讀取Excel表格,并封裝了一個OpExcel類來實現(xiàn)各種常用的功能,包括獲取表格行數(shù)、列數(shù)、指定行列數(shù)據(jù)、指定單元格數(shù)據(jù)、合并單元格、特定數(shù)據(jù)類型單元格檢查等。這些方法可以幫助你更加便捷地處理Excel表格。