如何与XLSXWriter一起工作而无需将文件保存在磁盘中

发布于 2025-01-27 21:40:10 字数 479 浏览 5 评论 0原文

我希望能够从我拥有的一些数据中创建一个Excel文件。准备就绪后,我想使用Python Telegram机器人发送它并乘坐文件。

理想情况下,将从划痕创建文件并将其保存到一个变量中,一旦使用Python Telegram Bot模块发送了文件,以发送文件并结束文件而无需将文件保存到磁盘中。

import xlsxWriter as xs

workbook = xs.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello')

workbook.close()

好的,所以在写入命令之后,我看不到文件夹中创建的任何文件,但是我不知道该文件是否在那里等待关闭而不是不存在。

hoy我可以不保存而做到吗

bot.send_file(my_xlsx,chat_id=1111111)

I want to be able to create an excel file from some data I have. Once it is ready, I want to send it using a python telegram bot and get ride of the file.

Ideally, the file will be created from scratch and saved into a variable, and once it is done sent using the python telegram bot module to send the file and end it without saving the file to the disk.

import xlsxWriter as xs

workbook = xs.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello')

workbook.close()

Ok so after the write command I don't see any file created in the folder, but I don't know if the file is there waiting to be closed instead of not existing.

Hoy can I, without saving it, do

bot.send_file(my_xlsx,chat_id=1111111)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

司马昭之心 2025-02-03 21:40:10

不将文件保存在磁盘中的一种方法是将其写入字节对象。 Bytesio是一个缓冲区,因此Excel信息存储在内存中:

import xlsxwriter as xs
from io import BytesIO

excel_io = BytesIO()

workbook = xs.Workbook(excel_io)
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello')

workbook.close()

您可以使用方法getValue() 。有关字节的更多信息(和io一般):

One way to not save the file in disk is writing it in a BytesIO object. BytesIO is a buffer, so the excel information is stored in memory:

import xlsxwriter as xs
from io import BytesIO

excel_io = BytesIO()

workbook = xs.Workbook(excel_io)
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello')

workbook.close()

You can get the data stored using the method getvalue(). More info about BytesIO (and io library in general) here:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文