1、安裝xlrd模塊:python
一、mac下打開終端輸入命令:ui
pip install xlrd
二、驗證安裝是否成功:spa
不報錯說明模塊安裝成功code
2、經常使用方法:對象
一、導入模塊:blog
import xlrd
二、打開文件:索引
x1 = xlrd.open_workbook("data.xlsx")
三、獲取sheet:ip
# -*- coding:utf-8 -*- import xlrd import os filename = "demo.xlsx" filePath = os.path.join(os.getcwd(), filename) print filePath # 一、打開文件 x1 = xlrd.open_workbook(filePath) # 二、獲取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) # 經過索引查找
輸出:utf-8
sheet_names: [u' plan', u'team building', u'modile', u'test'] sheet_number: 4 sheet_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>
四、獲取sheet的彙總數據:字符串
# -*- coding:utf-8 -*- import xlrd import os from datetime import date,datetime filename = "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 name print "row num:", sheet1.nrows # get sheet all rows number print "col num:", sheet1.ncols # get sheet all columns number
輸出:
sheet name: plan row num: 31 col num: 11
# -*- coding:utf-8 -*- import xlrd import os from datetime import date,datetime filename = "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) 表操做
# -*- coding:utf-8 -*- import xlrd import os from datetime import date,datetime filename = "demo.xlsx" filePath = os.path.join(os.getcwd(), filename) print filePath # 一、打開文件 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])
# -*- coding:utf-8 -*- import xlrd import os from datetime import date,datetime filename = "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).value print sheet1.row(1)[2].value #取類型 print sheet1.cell(1, 2).ctype print sheet1.cell_type(1, 2) print sheet1.row(1)[2].ctype
# -*- coding:utf-8 -*- import xlrd import os filename = "demo.xlsx" filePath = os.path.join(os.getcwd(), filename) # 打開文件 x1 = xlrd.open_workbook(filePath) sheet1 = x1.sheet_by_name("plan") # (0,0)轉換成A1 print xlrd.cellname(0, 0) # (0,0)轉換成A1 print xlrd.cellnameabs(0, 0) # (0,0)轉換成$A$1 print xlrd.colname(30) # 把列由數字轉換爲字母表示
輸出:
A1 $A$1 AE