您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python中的xlrd模塊使用方法”,在日常操作中,相信很多人在Python中的xlrd模塊使用方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python中的xlrd模塊使用方法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
xlrd是讀取excel表格數據;
支持 xlsx和xls 格式的excel表格;
三方模塊安裝方式:pip3 install xlrd;
模塊導入方式: import xlrd
Xler的操作主要分兩步:
其一時獲取book對象,
其二book對象再次進行excel的讀取操作。
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)
Names = Workbook.sheet_names()
workbook = xlrd.open_workbook("C:\\Users\li\Desktop\測試用例.xlsx") names = workbook.sheet_names() print(names)
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)
通過文件名
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))
①獲取所有行數
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)
①獲取有效列數
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)
①獲取單元格數據對象。 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)
單元類型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模塊使用方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。