python-3.xtelegramopenpyxlaiogram

Openpyxl, Telegram. Selecting the active sheet in the for loop


Using openpyxl I get the file(reading it). Using the cycle, I get all the names of the sheets. Next, I make a command by which I output data from the sheet

import openpyxl

from aiogram import Router, F
from aiogram.types import Message
from aiogram.filters import Command, CommandStart

from keyboards import reply, inline

router = Router()
wb = openpyxl.reader.excel.load_workbook(filename = "data/Inventarization 2023.xlsx", data_only = True)

for item in wb.sheetnames:
sheet = wb[item]

@router.message((F.text == item))
async def agent (message: Message):
print(sheet)
text = ""
for i in range (11, 312):
text += f"🔢 № {sheet['B' + str(i)].value}\n📅 Дата ввода: {sheet['C' + str(i)].value}\n✏️ Серийный номер: {sheet['D' + str(i)].value}\n📌 Инвентарный номер: {sheet['E' + str(i)].value}\n📝 Наименование: {sheet['F' + str(i)].value}\n🚪 Кабинет: {sheet['G' + str(i)].value}\n📚 Примечание: {sheet['H' + str(i)].value}\n\n"
for x in range(0, len(text), 4096):
mess = text[x: x + 4096]        
await message.answer(mess)

I tried to transfer the command to the loop, but it didn't help


Solution

  • It turned out to be much easier. In Router, it was necessary to use message.text instead of item

    User moken is right that if openpyxl outputs everything correctly in the loop, then it's already in aiogram