discord.py - 发送虚拟 PDF 文件



有没有办法将虚拟PDF文件上传到Discord(我不希望在计算机上创建文件(。我已经知道我可以使用这样的io.StringIO发送虚拟txt文件:

import discord, asyncio
from discord.ext import commands
from io import StringIO
bot = commands.Bot()
@bot.command()
async def send(ctx, *, string):
await ctx.send(file=discord.File(
fp=StringIO("This is a test"),
filename="Test.txt"
)

但这不适用于 PDF 文件。我尝试使用io.BytesIO,但没有得到任何结果。有人知道如何解决这个问题吗?

下面是一个使用 FPDF 的示例:

import discord
from discord.ext import commands
from io import BytesIO
from fpdf import FPDF
bot = commands.Bot("!")
@bot.command()
async def pdf(ctx, *, text):
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(40, 10, text)
bstring = pdf.output(dest='S').encode('latin-1')
await ctx.send(file=discord.File(BytesIO(bstring), filename='pdf.pdf'))
bot.run("token")

最新更新