python读取Excel实例详细教程
生活随笔
收集整理的這篇文章主要介紹了
python读取Excel实例详细教程
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.操作步驟:
(1)安裝python官方Excel庫-->xlrd
(2)獲取Excel文件位置并讀取(3)讀取sheet
(4)讀取指定rows和cols內(nèi)容
2.示例代碼:
# -*- coding: utf-8 -*-import xlrdfrom datetime import date,datetimedef read_excel():#文件位置ExcelFile=xlrd.open_workbook(r'C:\Users\Administrator\Desktop\TestData.xlsx')#獲取目標(biāo)EXCEL文件sheet名print ExcelFile.sheet_names()#------------------------------------#若有多個sheet,則需要指定讀取目標(biāo)sheet例如讀取sheet2#sheet2_name=ExcelFile.sheet_names()[1]#------------------------------------#獲取sheet內(nèi)容【1.根據(jù)sheet索引2.根據(jù)sheet名稱】#sheet=ExcelFile.sheet_by_index(1)sheet=ExcelFile.sheet_by_name('TestCase002')#打印sheet的名稱,行數(shù),列數(shù)print sheet.name,sheet.nrows,sheet.ncols#獲取整行或者整列的值rows=sheet.row_values(2)#第三行內(nèi)容cols=sheet.col_values(1)#第二列內(nèi)容print cols,rows#獲取單元格內(nèi)容print sheet.cell(1,0).value.encode('utf-8')print sheet.cell_value(1,0).encode('utf-8')print sheet.row(1)[0].value.encode('utf-8')#打印單元格內(nèi)容格式print sheet.cell(1,0).ctypeif__name__ =='__main__':read_excel()總結(jié)
以上是生活随笔為你收集整理的python读取Excel实例详细教程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 20 秒画完小猪佩奇“社会
- 下一篇: Python多进程及多参数的处理方法