问:OpenPyxl 检查现有行然后更新单元格
Q: OpenPyxl checking for existing row and then updating cell
我想检查现有电子表格中的名称列,如果它存在,我想用该行的时间戳更新特定列。我陷入了困境,因为我无法弄清楚如何在没有 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
任何帮助将不胜感激。
想通了
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
我想检查现有电子表格中的名称列,如果它存在,我想用该行的时间戳更新特定列。我陷入了困境,因为我无法弄清楚如何在没有 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
任何帮助将不胜感激。
想通了
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