[英]How to find cell in a column that contains a string and return value of a cell in another of row?
If the cell contains "external"
from the C
column then copy cell "good"
from the D
column, into the E
column, in the rows where the A
column contains 003
.如果单元格包含来自
C
列的"external"
,则将单元格"good"
从D
列复制到E
列中,在A
列包含003
的行中。
Below are two images (before and after) in excel.以下是excel中的两张图像(之前和之后)。
Before:前:
After:后:
I tried to find a correct script but it did not work out.我试图找到一个正确的脚本,但没有成功。 It needs to be changed to "row" and "column" where I put "???"
它需要更改为“行”和“列”,我在其中放置“???” :
:
import openpyxl
from openpyxl import load_workbook
wb_source = openpyxl.load_workbook("path/file.xlsx")
sheet = wb_source['Sheet1']
x=sheet.max_row
y=sheet.max_column
for r in range(1, x+1) :
for j in range(1, y+1):
copy(sheet.cell(row= ???, column=???)
if str(copy.value)=="external":
sheet.??
break
wb_source.save("path/file2.xlsx")
How should they be added (row and column)?它们应该如何添加(行和列)?
Try:尝试:
import openpyxl
wb = openpyxl.load_workbook("file1.xlsx")
ws = wb['Sheet1']
data = list()
for r, row in enumerate(ws.iter_rows()):
data.append([cell.value for c, cell in enumerate(row)])
mapper = {l[0]: l[-1] for l in data if l[2]=="external"}
for r, row in enumerate(ws.iter_rows()):
if ws.cell(r+1, 1).value in mapper:
ws.cell(r+1, 5).value = mapper[ws.cell(r+1, 1).value]
wb.save("file2.xlsx")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.