discord.py - 正在发送虚拟 PDF 文件
discord.py - Sending virtual PDF file
有什么方法可以将虚拟 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")
有什么方法可以将虚拟 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")