簡體   English   中英

如何在Excel工作表中查找值,然后使用openpyxl在同一行的不同單元格中寫入另一個值

[英]How to find a value in excel sheet and then write another value in a different cell in the same row with openpyxl

我正在嘗試在Excel文件的行中搜索特定值。 找到匹配的值后,我想在同一行中找到的值旁邊的列中寫入另一個值。

例如,如果我要查找單詞“ test”並在單元格A3中找到它,那么我想在單元格B3中打印一個指定的值,例如6。

如果有所不同,我確實希望只需要在A列中搜索值,並始終將其他值打印到同一行的B列中。

這是我到目前為止的代碼。

wb = openpyxl.load_workbook(path)
ws = wb.get_sheet_by_name('Sheet')
for row in ws.iter_rows():
    for cell in row:
        if cell.value == "test":
            ws.cell(row=cell, column=2).value = 6
wb.save(path)

任何幫助將不勝感激!

ws.cell(row=cell, column=2).value = 6

應該

 ws.cell(row=cell, column=2, value=6)

更改此行

 ws.cell(row=cell, column=2).value = 6

 ws.cell(row=cell.Row, column=2).value = 6

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM