一介评, python安裝xlrd模塊
1, cmd下安裝:
pip3 install xlrd
二, 使用模塊
1, 導(dǎo)入模塊
import xlrd
2, 打開文件
data = xlrd.open_workbook(path)
3, 獲取工作表
sheet = data.sheets()[index] #根據(jù)索引獲取表
sheet = data.sheet_by_index(sheet_index) #根據(jù)索引獲取表
sheet = data.sheet_by_name(sheet_name) #根據(jù)名字獲取表
sheet_names = data.sheet_names() #獲取所有表名
4,數(shù)據(jù)類型
0: empty
1: string(text)
2: number
3: date
4: boolean
5: error
6: blank
5, 行操作
row_length = data.nrows #獲取行長度
data.row_len(rowx) #獲取行長度
data.row(rowx) #返回該行的對象數(shù)據(jù)列表
data.row_slice(rowx) #返回該行的對象數(shù)據(jù)列表
data.row_types(rowx, start_colx=0, end_colx=None) #返回該行的類型數(shù)據(jù)列表
data.row_values(rowx, start_colx=0, end_colx=None) #返回該行的值數(shù)據(jù)列表
6, 列操作
col_length = data.ncols #獲取列長度
data.col_len(colx) #獲取列長度
data.col(colx) #返回該列的對象數(shù)據(jù)列表
data.col_slice(colx) #返回該列的對象數(shù)據(jù)列表
data.col_types(colx, start_rowx=0, end_rowx=None) #返回該列的類型數(shù)據(jù)列表
data.col_values(colx, start_rowx=0, end_rowx=None) #返回該列的值數(shù)據(jù)列表
7要尔, 單元格
data.cell(rowx, colx) #返回該單元格 數(shù)據(jù)類型:數(shù)據(jù)
data.cell(rowx,colx).value #返回該單元格 數(shù)據(jù)
data.cell(rowx,colx).ctype #返回該單元格 數(shù)據(jù)類型
data.cell_value(rowx,colx) #返回該單元格 數(shù)據(jù)
data.cell_type(rowx,colx) #返回該單元格 數(shù)據(jù)類型
xlrd 1.2.0以上版本不支持xlsx文件项玛,有點(diǎn)無語,會出現(xiàn)報(bào)錯:XLRDError: Excel xlsx file; not supported
推薦使用 openpyx
python使用openpyxl讀寫excel基礎(chǔ) - 簡書 (jianshu.com)