I want to use OpenPyXL to search for a specific string and put any value in the cell next to it.

Asked 2 years ago, Updated 2 years ago, 289 views

I'm a super beginner at Python.

Load the prepared Excel using openpyxl and
You would like to enter any number next to a cell containing a specific character.

In the image below, the target is indicated, but
if a specific string is included. I would like to enter any number in the cell next to me.

If you have any ideas to realize them, please let me know.

Enter a description of the image here

python openpyxl

2022-09-30 22:03

1 Answers

from openpyxl import load_workbook
wb=load_workbook("test.xlsx")
ws = wb.active
target_col="A"
next_col="B"
target_string = "target"
for row in range(2,ws.max_row+1):
    if target_string instr(ws[f"{target_col}{row}"].value):
        ws [f"{next_col}{row}"] = 1
wb.save("test_copy.xlsx")


2022-09-30 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.