Home > OS >  Sort and change values in Excel with Python
Sort and change values in Excel with Python

Time:11-01

I'm new to python and this is really challenging. Much appreciate if you can help.

I have an Excel file with some sample data like bellow.

Sample data sheet

And I need to filter by given company name and replace the products according to previous results.

Ex :- Filter the company column by "ABC" and replace the "P1" and "P4" to "ABC1" in product column.

CodePudding user response:

You need to loop over the rows and find if the company column's value is matching with the value you are trying to filter with.Then change the value of the expected column of that row .

you can check this link to find out how you can match with the company name How to filter column data using openpyxl

To set value in the cell you can follow this article how to write to a new cell in python using openpyxl

CodePudding user response:

Here is how I solved the problem:

import openpyxl

wb1 = openpyxl.load_workbook('Book1.xlsx')
sh1 = wb1["Sheet1"]


for row in sh1.iter_rows():
    if row[3].value == "ABC":
        row[2].value = "ABC1"
     
wb1.save("Book1.xlsx")

Thank you.

  • Related