Title:
There is a table file named Producesales.xlsx, each line represents a separate sales record, the first column (a) is the product name, the second line (B) is the product price, the third line (C) is the quantity of the sale, and the fourth line (D) is the total revenue of the sale (calculated automatically based on unit price and sales quantity, when B , the C column changes will automatically calculate the new value).
Now suppose the table Celery,garlic,lemon the price of these three items is set incorrectly, please update the table to set a new price:
Celery 1.19Garlic 3.07Lemon 1.27
Code
import openpyxldef 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(‘修改数据成功......‘)
Run
Python's implementation of changes to Excel table data