批量更改商品价格

每一行代表一次单独的销售。列分别是销售产品的类型(A)、产品每磅的价格
(B)、销售的磅数(C),以及这次销售的总收入。TOTAL 列设置为 Excel 公式,将每磅的成本乘以销售的磅数,
并将结果取整到分。有了这个公式,如果列 B 或 C 发生变化,TOTAL 列中的单元格将自动更新.

需要更新的价格如下:
Celery 1.19
Garlic 3.07
Lemon 1.27

现在假设 Garlic、 Celery 和 Lemons 的价格输入的不正确。这让你面对一项无聊
的任务:遍历这个电子表格中的几千行,更新所有 garlic、celery 和 lemon 行中每磅
的价格。你不能简单地对价格查找替换,因为可能有其他的产品价格一样,你不希
望错误地“更正”。对于几千行数据,手工操作可能要几小时。

import openpyxl
def changeGoods(wbname,name,price):
    wb = openpyxl.load_workbook(wbname)
    sheet = wb.active
    info = []
    for row in sheet.rows:
        ls = [cell.value for cell in row]
        info.append([cell.value for cell in row])
    for ls in info:
        if ls[0] == name:
            ls[1] = price
    for row , item in enumerate(info):
        for column,cellValue in enumerate(item):
            sheet.cell(row = row+1,column = column+1,value = cellValue)
    wb.save(wbname)
    print("%s价格更新%s成功" %(name,price))
changeGoods('produceSales.xlsx','Garlic',3.07)
changeGoods('produceSales.xlsx','Celery',1.19)
changeGoods('produceSales.xlsx','Lemon',1.27)

这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_42687283/article/details/82534183