Python3 讀取和寫入excel xlsx檔案 使用openpyxl

來源:互聯網
上載者:User

標籤:第一個   大量   date   window   log   類型   易用   mes   優雅   


python處理excel已經有大量包,主流代表有:

?xlwings:簡單強大,可替代VBA

?openpyxl:簡單易用,功能廣泛

?pandas:使用需要結合其他庫,資料處理是pandas立身之本

?win32com:不僅僅是excel,可以處理office;不過它相當於是 windows COM 的封裝,新手使用起來略有些痛苦。

?Xlsxwriter:豐富多樣的特性,缺點是不能開啟/修改已有檔案,意味著使用 xlsxwriter 需要從零開始。

?DataNitro:作為外掛程式內嵌到excel中,可替代VBA,在excel中優雅的使用python

?xlutils:結合xlrd/xlwt,老牌python包,需要注意的是你必須同時安裝這三個庫

 

 

1.openpyxl使用

openpyxl是Python可以用於處理xlsx的庫!

2.openpyxl安裝

 

pip install openpyxl

 

 

3.提示

(1)開啟excel檔案,擷取工作表

import openpyxl

wb=openpyxl.load_workbook(‘ttt.xlsx‘)  #開啟excel檔案

print(wb.get_sheet_names())  #擷取活頁簿所有工作表名

 

sheet=wb.get_sheet_by_name(‘Sheet1‘)  #擷取工作表

print(sheet.title) 

 

sheet02=wb.get_active_sheet()  #擷取活動的工作表

print(sheet02.title)

 

(2)操作儲存格

 

print(sheet[‘A1‘].value)  #擷取儲存格A1值

print(sheet[‘A1‘].column)  #擷取儲存格列值

print(sheet[‘A1‘].row)  #擷取儲存格行號

 

print(sheet.cell(row=1,column=1).value)  #擷取儲存格A1值,column與row依然可用

 

for i in range(1,4,1):

    print(sheet.cell(row=i,column=1).value) #更加方便實用

 

print(sheet.max_column)  #擷取最大列數

print(sheet.max_row)  #擷取最大行數

 

(3)讀取excel檔案

#wbname==即檔案名稱,sheetname==工作表名稱,可以為空白,若為空白預設第一個工作表

def readwb(wbname,sheetname):

    wb=openpyxl.load_workbook(filename=wbname,read_only=True)

    if (sheetname==""):

        ws=wb.active

    else:

        ws=wb[sheetname]

    data=[]

    for row in ws.rows:

        list=[]

        for cell in row:

            aa=str(cell.value)

            if (aa==""):

                aa="1"

            list.append(aa)

        data.append(list)

 

    print (wbname +"-"+sheetname+"- 已成功讀取")

    return data

 

(4)建立excel,並寫入資料

#建立excel

def creatwb(wbname):  

    wb=openpyxl.Workbook()

    wb.save(filename=wbname)

    print ("建立Excel:"+wbname+"成功")

 

# 寫入excel檔案中 date 資料,date是list資料類型, fields 表頭

def savetoexcel(data,fields,sheetname,wbname):   

    print("寫入excel:")

    wb=openpyxl.load_workbook(filename=wbname)

 

    sheet=wb.active

    sheet.title=sheetname  

 

    field=1

    for field in range(1,len(fields)+1):   # 寫入表頭

        _=sheet.cell(row=1,column=field,value=str(fields[field-1]))

 

    row1=1

    col1=0

    for row1 in range(2,len(data)+2):  # 寫入資料

        for col1 in range(1,len(data[row1-2])+1):

            _=sheet.cell(row=row1,column=col1,value=str(data[row1-2][col1-1]))

 

    wb.save(filename=wbname)

    print("儲存成功")

Python3 讀取和寫入excel xlsx檔案 使用openpyxl

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.