標籤:轉換 分享圖片 empty dem tool pen learning 合并 複製
一、安裝xlrd模組:
1、mac下開啟終端輸入命令:
pip install xlrd
2、驗證安裝是否成功:
- 在mac終端輸入 python 進入python環境
- 然後輸入 import xlrd
不報錯說明模組安裝成功
二、常用方法:
1、匯入模組:
import xlrd
2、開啟檔案:
x1 = xlrd.open_workbook("data.xlsx")
3、擷取sheet:
- 擷取所有sheet名字:x1.sheet_names()
- 擷取sheet數量:x1.nsheets
- 擷取所有sheet對象:x1.sheets()
- 通過sheet名尋找:x1.sheet_by_name("test”)
- 通過索引尋找:x1.sheet_by_index(3)
# -*- coding:utf-8 -*-import xlrdimport osfilename = "demo.xlsx"filePath = os.path.join(os.getcwd(), filename)print filePath# 1、開啟檔案x1 = xlrd.open_workbook(filePath)# 2、擷取sheet對象print ‘sheet_names:‘, x1.sheet_names() # 擷取所有sheet名字print ‘sheet_number:‘, x1.nsheets # 擷取sheet數量print ‘sheet_object:‘, x1.sheets() # 擷取所有sheet對象print ‘By_name:‘, x1.sheet_by_name("test") # 通過sheet名尋找print ‘By_index:‘, x1.sheet_by_index(3) # 通過索引尋找
輸出:
sheet_names: [u‘ plan‘, u‘team building‘, u‘modile‘, u‘test‘]sheet_number: 4sheet_object: [<xlrd.sheet.Sheet object at 0x10244c190>, <xlrd.sheet.Sheet object at 0x10244c150>, <xlrd.sheet.Sheet object at 0x10244c110>, <xlrd.sheet.Sheet object at 0x10244c290>]By_name: <xlrd.sheet.Sheet object at 0x10244c290>By_index: <xlrd.sheet.Sheet object at 0x10244c290>
4、擷取sheet的摘要資料:
- 擷取sheet名:sheet1.name
- 擷取總行數:sheet1.nrows
- 擷取總列數:sheet1.ncols
# -*- coding:utf-8 -*-import xlrdimport osfrom datetime import date,datetimefilename = "demo.xlsx"filePath = os.path.join(os.getcwd(), filename)print filePath# 開啟檔案x1 = xlrd.open_workbook(filePath)# 擷取sheet的摘要資料sheet1 = x1.sheet_by_name("plan")print "sheet name:", sheet1.name # get sheet nameprint "row num:", sheet1.nrows # get sheet all rows numberprint "col num:", sheet1.ncols # get sheet all columns number
輸出:
sheet name: planrow num: 31col num: 11
5、儲存格批量讀取: a)行操作:
- sheet1.row_values(0) # 擷取第一行所有內容,合併儲存格,首行顯示值,其它為空白。
- sheet1.row(0) # 擷取儲存格實值型別和內容
- sheet1.row_types(0) # 擷取儲存格資料類型
# -*- coding:utf-8 -*-import xlrdimport osfrom datetime import date,datetimefilename = "demo.xlsx"filePath = os.path.join(os.getcwd(), filename)x1 = xlrd.open_workbook(filePath)sheet1 = x1.sheet_by_name("plan")# 儲存格批量讀取print sheet1.row_values(0) # 擷取第一行所有內容,合併儲存格,首行顯示值,其它為空白。print sheet1.row(0) # 擷取儲存格實值型別和內容print sheet1.row_types(0) # 擷取儲存格資料類型
輸出:
[u‘learning plan‘, u‘‘, u‘‘, u‘‘, u‘‘, u‘‘, u‘‘, u‘‘, 123.0, 42916.0, 0][text:u‘learning plan‘, empty:u‘‘, empty:u‘‘, empty:u‘‘, empty:u‘‘, empty:u‘‘, empty:u‘‘, empty:u‘‘, number:123.0, xldate:42916.0, bool:0]array(‘B‘, [1, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4])
b) 表操作
- sheet1.row_values(0, 6, 10) # 取第1行,第6~10列(不含第10表)
- sheet1.col_values(0, 0, 5) # 取第1列,第0~5行(不含第5行)
- sheet1.row_slice(2, 0, 2) # 擷取儲存格實值型別和內容
- sheet1.row_types(1, 0, 2) # 擷取儲存格資料類型
# -*- coding:utf-8 -*-import xlrdimport osfrom datetime import date,datetimefilename = "demo.xlsx"filePath = os.path.join(os.getcwd(), filename)print filePath# 1、開啟檔案x1 = xlrd.open_workbook(filePath)sheet1 = x1.sheet_by_name("plan")# 列操作print sheet1.row_values(0, 6, 10) # 取第1行,第6~10列(不含第10表)print sheet1.col_values(0, 0, 5) # 取第1列,第0~5行(不含第5行)print sheet1.row_slice(2, 0, 2) # 擷取儲存格實值型別和內容,同sheet1.row(0)print sheet1.row_types(1, 0, 2) # 擷取儲存格資料類型
輸出:
[u‘‘, u‘‘, 123.0, 42916.0][u‘learning plan‘, u‘\u7f16\u53f7‘, 1.0, 2.0, 3.0][number:1.0, text:u‘\u7ba1\u7406\u5b66\u4e60‘]array(‘B‘, [1, 1])
6、特定儲存格讀取: a) 擷取儲存格值:
- sheet1.cell_value(1, 2)
- sheet1.cell(1, 2).value
- sheet1.row(1)[2].value
b) 擷取儲存格類型:
- sheet1.cell(1, 2).ctype
- sheet1.cell_type(1, 2)
- sheet1.row(1)[2].ctype
# -*- coding:utf-8 -*-import xlrdimport osfrom datetime import date,datetimefilename = "demo.xlsx"filePath = os.path.join(os.getcwd(), filename)x1 = xlrd.open_workbook(filePath)sheet1 = x1.sheet_by_name("plan")# 特定儲存格讀取# 取值print sheet1.cell_value(1, 2)print sheet1.cell(1, 2).valueprint sheet1.row(1)[2].value#取類型print sheet1.cell(1, 2).ctypeprint sheet1.cell_type(1, 2)print sheet1.row(1)[2].ctype 7、(0,0)轉換A1:
- xlrd.cellname(0, 0) # (0,0)轉換成A1
- xlrd.cellnameabs(0, 0) # (0,0)轉換成$A$1
- xlrd.colname(30) # 把列由數字轉換為字母表示
# -*- coding:utf-8 -*-import xlrdimport osfilename = "demo.xlsx"filePath = os.path.join(os.getcwd(), filename)# 開啟檔案x1 = xlrd.open_workbook(filePath)sheet1 = x1.sheet_by_name("plan")# (0,0)轉換成A1print xlrd.cellname(0, 0) # (0,0)轉換成A1print xlrd.cellnameabs(0, 0) # (0,0)轉換成$A$1print xlrd.colname(30) # 把列由數字轉換為字母表示
輸出:
A1$A$1AE
8、資料類型:
- 空:0
- 字串:1
- 數字:2
- 日期:3
- 布爾:4
- error:5
python讀取excel(xlrd)