import time
import pandas as pd
import zipfile
from io import BytesIO, StringIO
memory_file = BytesIO() # 创建二进制内存文件
dl_name = '{}.zip'.format("文件下载_" + time.strftime('%Y%m%d', time.localtime())) # 下载文件zip名称
file_name = '{}.csv'.format("文件下载_" + time.strftime('%Y%m%d', time.localtime())) # csv文件名称
in_memory_file = StringIO() # csv内存文件
df.to_csv(in_memory_file, index=False)
in_memory_file.seek(0)
with zipfile.ZipFile(memory_file, "a", zipfile.ZIP_DEFLATED) as zf:
zf.writestr(file_name, in_memory_file.getvalue().encode('utf_8_sig'))
memory_file.seek(0)
file_size = math.ceil(sys.getsizeof(memory_file) / (1024 * 1024)) # 获取文件大小
workbook = (dl_name, memory_file.read()) #返回文件名和zip的Bytes文件