Python's implementation of changes to Excel table data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.