溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python之實現(xiàn)對excel表格數(shù)據(jù)的修改

發(fā)布時間:2020-07-25 11:52:04 來源:網(wǎng)絡 閱讀:12554 作者:長安223 欄目:編程語言

題目:
有一個名為produceSales.xlsx的表格文件,每一行代表一次單獨的銷售紀錄,第一列(A)是產(chǎn)品名字,第二行(B)是產(chǎn)品價格,第三行(C)是銷售的數(shù)量,第四行(D)是本次銷售總收入(根據(jù)單價和銷售數(shù)量會自動計算,當B、C列變動會自動計算新的值)。

現(xiàn)在假設表格中Celery,Garlic,Lemon這三個商品的單價設置錯誤,請更新表格設置新的價格:

Celery      1.19
Garlic      3.07
Lemon       1.27

代碼

import openpyxl

def modify(sheet,name,value):
    for index,row in enumerate(sheet.rows):
        if name == sheet['A'+str(index+1)].value:
            sheet.cell(row=index+1,column=2,value=value)

try:
    wb = openpyxl.load_workbook('produceSales.xlsx')
    sheet = wb[wb.active.title]
    modify(sheet,'Celery',1.19)
    modify(sheet,'Garlic',3.07)
    modify(sheet,'Lemon',1.27)
    wb.save('produceSales.xlsx')
except Exception as e:
    print('修改表格出錯!','\n',e)
else:
    print('修改數(shù)據(jù)成功......')

運行截圖
python之實現(xiàn)對excel表格數(shù)據(jù)的修改

python之實現(xiàn)對excel表格數(shù)據(jù)的修改

python之實現(xiàn)對excel表格數(shù)據(jù)的修改

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI