python接口自動化--Excel

1.操做步驟:


(1)安裝python官方Excel庫-->xlrdpython

(2)獲取Excel文件位置並讀取spa

(3)讀取sheet3d

(4)讀取指定rows和cols內容excel

2.示例代碼

# -*- coding: utf-8 -*-
import xlrd

from datetime import date,datetime

def read_excel():
    ExcelFile=xlrd.open_workbook(r'C:\Users\Administrator\Documents\autoTest\testexcel.xlsx')

#獲取目標EXCEL文件sheet名
    print (ExcelFile.sheet_names())

#------------------------------------

#如有多個sheet,則須要指定讀取目標sheet例如讀取sheet2

#sheet2_name=ExcelFile.sheet_names()[1]

#------------------------------------

#獲取sheet內容【1.根據sheet索引2.根據sheet名稱】

#sheet=ExcelFile.sheet_by_index(1)

    sheet=ExcelFile.sheet_by_name('Sheet1')

#打印sheet的名稱,行數,列數

    print("name===rows==cols")
    print(sheet.name,sheet.nrows,sheet.ncols)
#獲取整行或者整列的值

    rows=sheet.row_values(2)#第3行內容

    cols=sheet.col_values(1)#第二列內容

    print(cols,rows)

#獲取單元格內容

    print(sheet.cell(1,0).value.encode('utf-8'))
    print(sheet.row(1)[0].value.encode('utf-8'))
    print(sheet.row(1)[1].value.encode('utf-8'))
    print(sheet.row(6)[1].value.encode('utf-8'))


if __name__ =="__main__":
    read_excel()

 

ctype介紹 :

0empty    1string     2number    3date    4boolean    5error

操做方式

sheet.row(6)[1].ctype(1)

  

相關文章
相關標籤/搜索