0

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.

Mr Hoona
  • 3
  • 1

2 Answers2

0

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

0

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.

joanis
  • 10,635
  • 14
  • 30
  • 40
Mr Hoona
  • 3
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 31 '21 at 13:45