See my code below. This code works very well, but I would like to do two things. One thing I did if an expression with or is much shorter than the actual one, for example. I have many such columns, not all are close to each other. I would like it to be shorter. Also, sometimes I may not know the exact letter in the column.
So, I want to know if there is a way to find out the column name or heading. Like the values that would be on the very top line. Therefore, I can check if this is one of these values, always execute the function in this cell if it is in the specified column. I cannot find the openpyxl function to create a column name. Not sure if he understands that the first line is different from the rest. I think, maybe, if it weren’t for me, I could try to run the test on the first line, but I don’t understand how to do it.
So is there a way to call the column name? or if there is no way to call the column name for validation, can someone help me do the validation on the first row to find out if it matters? then will you change the correct line I'm in? It makes sense.
So, instead of code:
if cellObj.column == 'H' or ...
He would say:
if cellObj.column_header == 'NameOfField or ...
Or, if this cannot be done, then:
if this cell has column where first row value is 'NameOfField' ...
, . stackoverflow , , , ( ).
for row in sheet.iter_rows():
for cellObj in row:
if cellObj.column == 'H' or cellObj.column == 'I' or cellObj.column == 'L' or cellObj.column == 'M':
print(cellObj.value),
if cellObj.value.upper() == 'OldValue1':
cellObj.value = 1
print(cellObj.value)
elif cellObj.value.upper() == 'OldValue2':
cellObj.value = 2
print(cellObj.value)