Q: OpenPyxl正在检查现有行,然后进行更新

2024-05-13 23:21:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想检查现有电子表格中的名称列,如果存在,我想用该行的时间戳更新特定列。我陷入了一个惯例,因为我不知道如何在没有for循环的情况下进行这项工作。for循环将为不匹配的行追加更多的行,当我在将名称与行匹配后尝试标记它时,列中不会显示任何内容。在

    for rowNum in range(2, ws1.max_row):
        log_name = ws1.cell(row=rowNum,column=1).value
        if log_name == chkout_new_name_text:
            print 'apple' + 'pen'
            ws1.cell(row=rowNum, column=2).value = str(time.strftime("%m/%d/%y %H:%M %p"))
            break
        else:
            continue
            print 'pen' + 'pineapple'
            # Normal procedure

任何帮助将不胜感激。在


Tags: name名称logforvalue时间cellcolumn
1条回答
网友
1楼 · 发布于 2024-05-13 23:21:47

弄明白了

    name_text = raw_input("Please enter name: ")
    matching_row_nbr = None
    for rowNum in range(2, ws1.max_row + 1 ):
        log_name = ws1.cell(row=rowNum,column=1).value
        if log_name == name_text:
            # Checks for a matching row and remembers the row number
            matching_row_nbr = rowNum
            break
    if matching_row_nbr is not None:
        # Uses the matching row number to change the cell value of the specific row
        ws1.cell(row=matching_row_nbr, column=6).value = str(time.strftime("%m/%d/%y %H:%M - %p"))
        wb.save(filename = active_workbook)
    else:
        # If the none of the rows match then continue with intended use of new data
        print name_text

相关问题 更多 >