python - 使用 openpyxl 移动到相邻单元格

标签 python excel openpyxl

我有一个算法可以在单元格中查找值,在本例中,假设单元格是 C10。我需要在 D 列中的旁边查找一个值,如果该值与我需要的值不匹配,则从该值向上移动一个单元格并检查是否匹配,等等。到目前为止我已经有了:

bits = [] 

for row in ws.iter_rows(row_offset=4,column_offset=3):
    #skip over empty rows
    if row:
        #current cell is in column C
        cell = row[2]
        try:
            #find the lowest address in the excel sheet
            if cell.internal_value == min(address):
                #somehow match up in column d
                for '''loop and search col D''':
                    if str(row[3].internal_value).upper == ('CONTROL 1' or 'CON 1'):
                        #add bits
                        for cell in row[4:]:
                            bits.append(cell.internal_value)    
        #pass over cells that aren't a number, ie values that will never match an address
        except ValueError:
            pass
        except TypeError:
            pass

有办法做到这一点吗?我知道使用 row[3] 的比较在 D 列中进行比较,但如果第一次不正确,我不知道如何进入该列。或者换句话说,更改 row[value] 中的值在行中移动,我需要知道什么值/如何在列中移动。

谢谢!

最佳答案

bits = []
min_address = False
for row in ws.iter_rows(row_offset=4,column_offset=3):
    c = row[2]
    d = row[3]
    if not d.internal_value: #d will always have a value if the row isn't blank
        if min_address: 
            break #bits is what you want it to be now
        bits = [] #reset bits every time we hit a new row
        continue #this will just skip to next row

    for bits_cell in row[4:]:
        if bits_cell.internal_value:
            bits.append(bits_cell.internal_value)
    if c.internal_value:
        if c.internal_value == min(address):
            min_address = True #we set it to true, then kept going until blank row

关于python - 使用 openpyxl 移动到相邻单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11162355/

相关文章:

python - 从Python中的16位无符号大端原始图像文件读取数据

.net - 从 Excel 应用程序对象中查找位数(32 位/64 位)?

python - 如何生成15个总和等于特定数的随机数

python - 在 Django Admin 中使字段唯一

Python Excel 模板读取和重写,维护公式和格式

excel - 将剪贴板数据复制到数组

"paste"运行时出现VBA运行时错误438

python - to_excel() 没有索引布局

python - 使用用公式编写的单元格中的值

Python Openpyxl 在 xlsx 中找不到数字