【轉載】Python操作Excel的讀取以及寫入

來源:互聯網
上載者:User

標籤: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的讀取以及寫入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.