如何在openpyxl Python的下一列中找到带有值的行并放置另一个值?

时间:2018-06-09 22:38:16

标签: python openpyxl

我正在研究电报机器人,它应该保存用户在excel电子表格中的响应。我正在使用telebot和openpyxl库。

问题是在列中搜索特定值(ID),然后使用行号填充下一列中的所有其他值。我对编码很新,所以我找不到任何解决方案。如果有人知道如何完成这项任务,请帮助我!提前谢谢。

这是我当前的python代码,用于更好地了解情况:

import telebot
import openpyxl

bot = telebot.TeleBot("TOKEN")

dest_filename = 'order_data.xlsx'
wb = openpyxl.load_workbook(dest_filename)
ws = wb['Sheet1']

def order_number_process(message):
    try:
       global key
       key = str(message.text)
       ws.cell(row = ws.max_row+1, column =1, value = key)
       wb.save(dest_filename)
       msg = bot.send_message(message.from_user.id, 'Order Number: '+key+ '\nSpecify date.', reply_markup=no_markup)
       bot.register_next_step_handler(msg, process_order_date)
    except Exception as e:
       error(message)

def process_order_date(message):

### Here is where the problem ###
    try:
        for rows in range(0, ws.max_row):
            for columns in range(0,1):
                rrow = key.get_row_number()
                value = str(message.text)
                ws.cell(row = rrow, column = 2, value = value)
#################################               

    bot.send_message(message.from_user.id, 'Order Date is Saved to the Order Number', reply_markup=no_markup)
    except Exception as e:
        error(message)

def error(message):
    bot.send_message(message.from_user.id, 'I do not understand',
                 reply_markup=start_menu)

@bot.message_handler(content_types=['text'])
def handle_text(message):
    if message.text == '/start':
        bot.send_message(message.from_user.id, 'Bot will track status of the order', reply_markup=start_menu)
    elif message.text == 'New Order':
        msg = bot.send_message(message.from_user.id, 'Send me the order number', reply_markup=no_markup)
    bot.register_next_step_handler(msg, order_number_process)
    else:
       bot.send_message(message.from_user.id, 'I don't understand',
                     reply_markup=start_menu)

bot.polling(none_stop=True, timeout=60)

0 个答案:

没有答案