简体   繁体   中英

Using openpyxl to change the value of all cells in a column based on the value that currently occupies those cells

I am completely new to openpyxl so, as you can imagine, I am having pretty hard times when I try to make use of it.

I have an Excel report that contains only one sheet (called Sheet1). I'd like to search all cells in one column (column H) for a specific string ("OK" and "QNS") and change that "OK" to "In Inventory" and "QNS" to "Exhausted".

I think my main confusion is exactly what to employ here. Is it a while loop combined with if/elif? Or do I need to iterate? Please help me understand how to do this. Thanks.

I believe I got this to work, but can someone check to see if this makes sense?

wb = load_workbook('C:/Users/User1/AppData/Local/Programs/Python/Python310/ March.xlsx')
ws = wb.active
conditionUpdate = {"OK": "In Inventory","Discrepancy: Subject": "In Inventory"}

for rowNum in range(2, ws.max_row):
    condition = ws.cell(row=rowNum, column=8).value
    if condition in conditionUpdate:
        ws.cell(row=rowNum, column=8).value = conditionUpdate[condition]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM