標籤:http fun html 迴圈 tps 通過 func error self
轉載來源:https://jingyan.baidu.com/article/e2284b2b754ac3e2e7118d41.html
#匯入包
import xlrd
#設定路徑
path=‘C:\\Users\\jyjh\\Desktop\\datap.xlsx‘
#開啟檔案
data=xlrd.open_workbook(path)
#查詢工作表
sheets=data.sheets()
sheets
可以通過函數、索引、名稱獲得工作表。
sheet_1_by_function=data.sheets()[0]
sheet_1_by_index=data.sheet_by_index(0)
sheet_1_by_name=data.sheet_by_name(u‘Sheet1‘)
可以通過方法獲得某一列或者某一行的數值。
sheet_1_by_name.row_values(1)
sheet_1_by_name.col_values(1)
通過工作表的屬性獲得行數和列數。
n_of_rows=sheet_1_by_name.nrows
n_of_cols=sheet_1_by_name.ncols
也可以用一個迴圈來遍曆一次檔案。
for i in range(n_of_rows):
print sheet_1_by_name.row_values(i)
可以通過以下的任意一種方式訪問儲存格的數值。
cell_A1=sheet_1_by_name.cell(0,0).value
cell_A1=sheet_1_by_name.row(0)[0].value
cell_A1=sheet_1_by_name.col(0)[0].value
最後通過以下的方法對儲存格的數值進行修改。
row=0
col=0
#ctype 0:empty,1:string,2:number,3:date,4:boolean,5:error
cell_type=1
value=‘Hello,Excel‘
cell_A1=sheet_1_by_name.cell(0,0).value
format=0
sheet_1_by_name.put_cell(row,col,cell_type,value,format)
cell_A1=sheet_1_by_name.cell(0,0).value
【轉載】Python操作Excel的讀取以及寫入