How to use field name or column header in openpyxl?

后端 未结 4 1065
情深已故
情深已故 2021-02-15 14:42

See my code below. This code works very well, but I would like to do two things. One thing is I made if statement with or much shorter than actual for example. I have many colum

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-15 15:15

    You can access cells from the first row and and column using the sheet.cell(row=#, column = #) syntax. For example:

    for row in enumerate(sheet.iter_rows()):
        for j, cellObj in enumerate(row):
            header_cell = sheet.cell(row=1, column=j)
    
            if cellObj.column in ['H', 'I', 'L', 'M', 'AA', 'AB']:
                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)
    

提交回复
热议问题