# encoding : utf-8 #設置編碼方式
import xlrd #導入xlrd模塊
#打開指定文件路徑的excel文件
xlsfile = r'D:\AutoPlan\apisnew.xls'
book = xlrd.open_workbook(xlsfile) #獲得excel的book對象
#獲取sheet對象,方法有2種:
sheet_name=book.sheet_names()[0] #獲得指定索引的sheet名字
print sheet_name
sheet1=book.sheet_by_name(sheet_name) #通過sheet名字來獲取,當然如果你知道sheet名字了可以直接指定
sheet0=book.sheet_by_index(0) #通過sheet索引獲得sheet對象
#獲取行數和列數:
nrows = sheet.nrows #行總數
ncols = sheet.ncols #列總數
#獲得指定行、列的值,返回對象爲一個值列表
row_data = sheet.row_values(0) #獲得第1行的數據列表
col_data = sheet.col_values(0) #獲得第一列的數據列表,然後就可以迭代裏面的數據了
#通過cell的位置座標獲得指定cell的值
cell_value1 = sheet.cell_value(0,1) ##只有cell的值內容,如:http://xxx.xxx.xxx.xxx:8850/2/photos/square/
print cell_value1
cell_value2 = sheet.cell(0,1) ##除了cell值內容外還有附加屬性,如:text:u'http://xxx.xxx.xxx.xxx:8850/2/photos/square/'
print cell_value2
python操作Excel
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.