Python3學習筆記31-xlrd模塊


xlrd模塊是用來讀取excel的第三方模塊,需要下載安裝后才能使用。新建一個excel,隨便填充一些數據用來測試下。

# -*- coding: utf-8 -*-
import xlrd
#打開excel文件讀取數據
exce = xlrd.open_workbook('C:/Users/ms/Desktop/test_xlrd.xlsx')

#獲取excel中對應的sheet
print('所有sheet名稱',exce.sheet_names()) #獲取所有sheet名稱
sheets = exce.sheets()  #獲取所有sheets
#sheet = exce.sheets()[0]    #也可以通過下標去訪問某個具體的sheet
sheet1 = exce.sheet_by_name('Sheet1')    #通過sheet名稱獲取
#sheet2 = exce.sheet_by_index(1) #通過下標獲取某個sheet

#獲取sheet中行數和列數
nrows = sheet1.nrows
ncols = sheet1.ncols
print('對應sheet中行數:%d行,列數:%d列'% (nrows,ncols))

#獲取sheet中整行或整列的數據(數組)
row1 = sheet1.row_values(3)     #通過下標獲取某一行的數據
col1 = sheet1.col_values(0)     #通過下標獲取某一列的數據
print('某行的數據:',row1)
print('某列的數據:',col1)

#獲取sheet中某個單元格的數據
cell_A3 = sheet1.cell(2,0).value    #第三行第一列
cell_B2 = sheet1.cell(1,1).value    #第二行第二列
cell_C3 = sheet1.cell(2,2).value    #第三行第三列
print('第一列第三行:',cell_A3)
print('第二行第二列:',cell_B2)
print('第三行第三列:',cell_C3)

#獲取單元格數據類型
A3_ctype = sheet1.cell(2,0).ctype   #數字類型
B2_ctype = sheet1.cell(1,1).ctype   #str類型
C3_ctype = sheet1.cell(2,2).ctype   #data類型
print('數字類型:',A3_ctype)
print('str類型:',B2_ctype)
print('data類型:',C3_ctype)
#ctype:0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error

在打印整行數據和整列數據的時候,合並的單元格,只會在合並的第一行或者第一列會有數據,之后打印出來都是空白。另外打印的日期時間也是錯誤的。

# -*- coding: utf-8 -*-
import xlrd
exce = xlrd.open_workbook('C:/Users/ms/Desktop/test_xlrd.xlsx')
sheet1 = exce.sheet_by_name('Sheet1')
print(sheet1.cell(4,0).value)
print(sheet1.cell(3,0).value)
print(sheet1.cell(3,2).value)
print(sheet1.cell(3,1).value)

先看合並單元格,這個沒有任何技巧。只能獲取 合並行單元格讀取行的第一個索引,合並列單元格讀取列的第一個索引。這樣才能讀到值,讀錯了就是空值。

但是合並單元格可能是讀到空值,excel本身也可能就存在空值。要怎么獲取單元格所謂的‘第一行或列的索引的’,這需要事先知道哪些單元格是合並的

print(sheet1.merged_cells)

使用merged_cells可以獲得合並單元格。返回的參數(row,row_range,col,col_range),返回的是行數索引,行數索引范圍,列數索引,列數索引范圍。注意這里返回的應該都是索引。

根據返回的這四個值可以計算出合並單元格范圍。計算時不需要把范圍算進去,比如(3,5,0,1)行數索引就是3,4.對應excel行數就是第四行,第五行。列數所以就是0,也就是第一列

而在取所謂的第一行或第一列索引時候,直接從返回的四個參數中,取第一個和第三個就行了。可以對照上面的代碼。對比下。

也可以封裝成一個方法

# -*- coding: utf-8 -*-
import xlrd
exce = xlrd.open_workbook('C:/Users/ms/Desktop/test_xlrd.xlsx')
sheet1 = exce.sheet_by_name('Sheet1')
def row_col(sheet):
    merge = []
    for(row,row_range,col,col_range) in sheet.merged_cells:
        merge.append([row,col])
    for index in merge:
        print(sheet.cell(index[0],index[1]).value)
row_col(sheet1)

 

 

再來看日期格式

# -*- coding: utf-8 -*-
import xlrd
exce = xlrd.open_workbook('C:/Users/ms/Desktop/test_xlrd.xlsx')
sheet1 = exce.sheet_by_name('Sheet1')

#處理單元格內容為data格式的數據
print(xlrd.xldate_as_datetime(sheet1.cell(2,2).value,0))    #轉換成日期格式
print(xlrd.xldate_as_tuple(sheet1.cell(2,2).value,0))   #返回元組

有兩種處理方式,一種轉換成日期,一種是轉換成元組

#如果ctype等於3,就用時間格式處理
def xldate_datetime(sheet,row,col):
    if(sheet.cell(row,col).ctype==3):
        date_value = xlrd.xldate_as_datetime(sheet.cell(row,col).value,0)
        return date_value
print(xldate_datetime(sheet1,2,2))

可以簡單封裝成一個方法、


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM