您的位置:首页 > 其它

pythton---批量更新商品信息

2018-09-09 00:04 90 查看

每一行代表一次单独的销售。列分别是销售产品的类型(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 readwb(wbname):
wb = openpyxl.load_workbook(wbname)
sheet = wb.active
all_info = []
for row in sheet.rows:
child = [cell.value for cell in row]
all_info.append(child)
for item1 in all_info:
if 'Celery' in item1:
item1[1]='1.19'
elif 'Garlic' in item1:
item1[1]='3.07'
elif 'Lemon' in item1:
item1[1] ='1.27'
else:
pass
return all_info
def save_to_excel(data, wbname, sheetname='sheet1'):
print("写入Excel[%s]中......." %(wbname))
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = sheetname

for row, item in enumerate(data):  # 0 [' BOOK', 50, 3]
for column, cellValue in enumerate(item): #  0 ' BOOK'
sheet.cell(row=row+1, column=column+1, value=cellValue)
wb.save(filename=wbname)
print("写入成功!")
data = readwb(wbname='produceSales.xlsx')
save_to_excel(data, wbname='produceSales.xlsx', sheetname="批量更新商品信息")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: