python-3.x Openpyxl,Telegram.在for循环中选择活动工作表[关闭]

zbq4xfa0  于 8个月前  发布在  Python
关注(0)|答案(1)|浏览(55)

已关闭。此问题需要details or clarity。目前不接受回答。
**要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

9天前关闭
Improve this question
使用openpyxl获取文件(阅读它)。使用循环,我获取所有工作表的名称。接下来,我创建一个命令,通过该命令从工作表输出数据

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)

字符串
我试着把命令转移到循环中,但是没有用

g6baxovj

g6baxovj1#

事实证明这要简单得多。在Router中,必须使用messages.text而不是item
用户moken是正确的,如果openpyxl在循环中正确输出所有内容,那么它已经在aigram中了

相关问题