Python中的xlrd模塊使用整理
1.簡述:
xlrd是讀取excel表格數據;
支持 xlsx和xls 格式的excel表格;
三方模塊安裝方式:pip3 install xlrd;
模塊導入方式: import xlrd
2.xlrd模塊的基本操作
Xler的操作主要分兩步:
其一時獲取book對象,
其二book對象再次進行excel的讀取操作。
2.1打開excel文件獲取book對象
xlrd.open_workbook(filename[,logfile,file_contents,…])
如果filename 文件名不存在,則會報錯 FilenotFoundError。
如果filename 文件名存在,則會返回一個xrld.book.Book 對象。 import xlrd
Workbook = xlrd.open_workbook("C:\\Users\li\Desktop\銀聯測試案例.xls") print(Workbook)
2.2獲取xlrd.book.Book 對象中的所有sheet名稱
Names = Workbook.sheet_names()
workbook = xlrd.open_workbook("C:\\Users\li\Desktop\測試用例.xlsx") names = workbook.sheet_names() print(names)
2.3獲取xlrd.book.Book對象中的所有sheet對象
Sheets = workbook.sheets()
workbook = xlrd.open_workbook("C:\\Users\li\Desktop\測試用例.xlsx") names = workbook.sheets() print(names)
獲取單個的sheet頁對象
三種方式 :
第一種 worksheet1 = workbook.sheet_by_index()
第二種 worksheet2 = workbook.sheet_by_name()
第三種 worksheet3 = workbook.sheets()[0]
workbook = xlrd.open_workbook("C:\\Users\lw\Desktop\測試用例.xlsx") sheets = workbook.sheets() worksheet1 = workbook.sheet_by_index(0) worksheet2 = workbook.sheet_by_name("公司分部") worksheet3 = workbook.sheets()[0] print(worksheet1,worksheet2,worksheet3)
2.4判斷xlrd.book.Book對象中某個sheet對象是否導入
通過文件名
workbook = xlrd.open_workbook("C:\\Users\lw\Desktop\測試用例.xlsx") sheets = workbook.sheets() print(workbook.sheet_loaded("公司分部"))
通過索引
workbook = xlrd.open_workbook("C:\\Users\lw\Desktop\測試用例.xlsx") sheets = workbook.sheets() print(workbook.sheet_loaded(0))
3.sheet對象的基本操作
3.1行操作
①獲取所有行數
Rows = sheet.nrows 特別註意,這是屬性而不是方法,不加括號。
workbook = xlrd.open_workbook("C:\\Users\lw\Desktop\測試用例.xlsx") sheets = workbook.sheets() worksheet1 = workbook.sheet_by_index(0) worksheet2 = workbook.sheet_by_name("公司分部") worksheet3 = workbook.sheets()[0] print(worksheet1.nrows)
②獲取某行的數據,值為列表形式
Value = sheet.row_values()
workbook = xlrd.open_workbook("C:\\Users\lw\Desktop\測試用例.xlsx") sheets = workbook.sheets() worksheet1 = workbook.sheet_by_index(0) worksheet2 = workbook.sheet_by_name("公司分部") worksheet3 = workbook.sheets()[0] value = worksheet1.row_values(1) print(value)
③獲取某行的類型及數據
Sheet.row()
workbook = xlrd.open_workbook("C:\\Users\li\Desktop\測試用例.xlsx") sheets = workbook.sheets() worksheet1 = workbook.sheet_by_index(0) worksheet2 = workbook.sheet_by_name("公司分部") worksheet3 = workbook.sheets()[0] value = worksheet1.row(1) print(value)
④獲取某行的類型的列表
Sheet.row_types()
單元類型ctype:empty為0,string為1,number為2,date為3,boolean為4, error為5(左邊為類型,右邊為類型對應的值);
workbook = xlrd.open_workbook("C:\\Users\li\Desktop\測試用例.xlsx") sheets = workbook.sheets() worksheet1 = workbook.sheet_by_index(0) worksheet2 = workbook.sheet_by_name("公司分部") worksheet3 = workbook.sheets()[0] value = worksheet1.row_types(1) print(value)
⑤以切片形式獲取某行的類型及數據
Sheet.row_slice() 記錄分隔符為\n
workbook = xlrd.open_workbook("C:\\Users\li\Desktop\測試用例.xlsx") sheets = workbook.sheets() worksheet1 = workbook.sheet_by_index(0) worksheet2 = workbook.sheet_by_name("公司分部") worksheet3 = workbook.sheets()[0] value = worksheet1.row_slice(1) print(value)
⑥獲取某行的長度
Sheet.len()
workbook = xlrd.open_workbook("C:\\Users\li\Desktop\測試用例.xlsx") sheets = workbook.sheets() worksheet1 = workbook.sheet_by_index(0) worksheet2 = workbook.sheet_by_name("公司分部") worksheet3 = workbook.sheets()[0] value = worksheet1.row_len(1) print(value)
⑦獲取sheet的所有生成器
Sheet.get_rows()
workbook = xlrd.open_workbook("C:\\Users\li\Desktop\測試用例.xlsx") sheets = workbook.sheets() worksheet1 = workbook.sheet_by_index(0) worksheet2 = workbook.sheet_by_name("公司分部") worksheet3 = workbook.sheets()[0] row = worksheet1.get_rows() for one in row: print(one)
3.2列操作
①獲取有效列數
Sheet.cols 註意:此處為屬性不加括號
②獲取某列數據
Sheet.values()
③獲取某列類型
Sheet.types()
單元類型ctype:empty為0,string為1,number為2,date為3,boolean為4, error為5(左邊為類型,右邊為類型對應的值);
④以slice切片方式獲取某列數據
Sheet.value_slice() workbook = xlrd.open_workbook("C:\\Users\li\Desktop\測試用例.xlsx") sheets = workbook.sheets() worksheet1 = workbook.sheet_by_index(0) worksheet2 = workbook.sheet_by_name("公司分部") worksheet3 = workbook.sheets()[0] cols = worksheet1.col value = worksheet1.col_values(0) type = worksheet1.col_types(0) valuesl = worksheet1.col_slice(0) print(cols) print("----------------------") print(value) print("----------------------") print(type) print("----------------------") print(valuesl)
3.3單元格操作
①獲取單元格數據對象。 sheet.cell(rowx,colx)類型為xlrd.sheet.Cell
②獲取單元格類型。Sheet.cell_type(rowx,colx)
單元類型ctype:empty為0,string為1,number為2,date為3,boolean為4, error為5(左邊為類型,右邊為類型對應的值);
③獲取單元格數據。
Sheet.cell_value(rowx,colx)
3.4對單元格中的日期/時間操作
單元類型ctype:empty為0,string為1,number為2,date為3,boolean為4, error為5(左邊為類型,右邊為類型對應的值);
①xlrd.xldate_as_tuple()
“{}-{:0>2}-{:0>2}”.format(date[0],date[1],date[2])
②xlrd.xldate_as_datetime(value,mode).strftime(“%Y-%m-%d”)
workbook = xlrd.open_workbook("C:\\Users\li\Desktop\測試用例.xlsx") import datetime sheet2_object = workbook.sheet_by_index(0) value_type = sheet2_object.cell(0, 1).ctype value_type = sheet2_object.cell_value(1, 4) data = xlrd.xldate.xldate_as_datetime(value_type,0) print(data.strftime("%Y-%m-%d")) date = xlrd.xldate.xldate_as_tuple(value_type,0) print("{}-{:0>2}-{:0>2}".format(date[0],date[1],date[2]))
到此這篇關於Python中的xlrd模塊使用整理的文章就介紹到這瞭,更多相關xlrd模塊的使用內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- python中xlrd模塊的使用詳解
- Python使用xlrd和xlwt實現自動化操作Excel
- 使用Python讀取和修改Excel文件(基於xlrd、xlwt和openpyxl模塊)
- python 使用xlsxwriter循環向excel中插入數據和圖片的操作
- Python對Excel進行處理的實操指南