Python基礎之excel

來源:互聯網
上載者:User

標籤:sheet   for   iter   ice   取資料   orm   寫入   write   .com   

python讀取excel主要是xlrd,python寫入和建立excel檔案可以用xlwt、XlsxWriter,xlwt支援office2013,XlsxWriter支援office2013及以上檔案。

建立和讀取excel代碼testXlrd.py:

#coding=utf-8import xlrdimport xlwt#通過xlrd讀取資料def readExcel():    filePath="d:\\a.xlsx"    data = xlrd.open_workbook(filePath)    table0 = data.sheets()[0]    nrows = table0.nrows    for i in range(nrows):        if i == 0: # 跳過第一行            continue        print(table0.row_values(i)[:5]) # 取前5列#通過xlwt寫入資料def writeExcel():    workbook = xlwt.Workbook() #注意Workbook的開頭W要大寫    sheet1 = workbook.add_sheet(‘sheet1‘)    #向sheet頁中寫入資料    sheet1.write(0,0,‘使用者名稱‘)    sheet1.write(0,1,‘郵箱‘)    sheet1.write(1,0,‘tom‘)    sheet1.write(1,1,‘[email protected]‘)    workbook.save(‘d:\\b.xls‘)    print(‘建立excel檔案完成!‘)

調用代碼:

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32Type "copyright", "credits" or "license()" for more information.>>> from testXlrd import *>>> writeExcel()建立excel檔案完成!>>> readExcel()[1.0, ‘joyet1‘][2.0, ‘joyet2‘][3.0, ‘joyet3‘][4.0, ‘joyet4‘][5.0, ‘joyet5‘][6.0, ‘joyet6‘][7.0, ‘joyet7‘][8.0, ‘joyet8‘][9.0, ‘joyet9‘]>>> 

 

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.