Python 如何压缩多个StringIO文件?
我有一个金字塔web应用程序,它使用内部web服务使用reportlab将一些数据转换为PDF文件。这非常有效,但一次只生成一个PDF文件。客户机现在希望能够打印(它可以压缩,而不是通过打印机实际打印)多个PDF文件 我目前在底部有一段代码:Python 如何压缩多个StringIO文件?,python,pyramid,Python,Pyramid,我有一个金字塔web应用程序,它使用内部web服务使用reportlab将一些数据转换为PDF文件。这非常有效,但一次只生成一个PDF文件。客户机现在希望能够打印(它可以压缩,而不是通过打印机实际打印)多个PDF文件 我目前在底部有一段代码: result = {"sales_order_data": {"sales_order_list": order_pdf_data}} encoded_result = urllib.urlencode(result) receiv
result = {"sales_order_data": {"sales_order_list": order_pdf_data}}
encoded_result = urllib.urlencode(result)
received_data = requests.post('http://convert_to_pdf/sales_order', data=encoded_result)
pdf_file = received_data.content
content_disposition = received_data.headers['Content-Disposition']
res = Response(content_type='application/pdf', content_disposition=content_disposition)
res.body = pdf_file
return res
pdf_文件是pdf文件的二进制形式。我想多次运行我的pdf转换代码,每次都将pdf二进制数据存储在一个排序列表中,然后使用StringIO和ZipFile将一组文件压缩到一起
我不太确定这是否可行:
list_of_pdf_files = []
for order in list_of_orders:
<processing of data here>
result = {"sales_order_data": {"sales_order_list": order_pdf_data}}
encoded_result = urllib.urlencode(result)
received_data = requests.post('http://convert_to_pdf/sales_order', data=encoded_result)
pdf_file = received_data.content
list_of_pdf_files.append(pdf_file)
zipped_file = <What do I do here to zip the list of pdf files?>
content_disposition = 'attachment; filename="pdf.zip"'
res = Response(content_type='application/zip', content_disposition=content_disposition)
res.body = zipped_file
return res
pdf文件列表=[]
对于订单列表中的订单:
结果={“销售订单数据”:{“销售订单列表”:订单pdf数据}
encoded_result=urllib.urlencode(结果)
接收到的数据=请求。post('http://convert_to_pdf/sales_order,数据=编码的结果)
pdf\u文件=收到的\u数据。内容
pdf文件列表。附加(pdf文件)
压缩文件=
内容\处置='附件;filename=“pdf.zip”'
res=响应(content\u type='application/zip',content\u disposition=content\u disposition)
res.body=压缩文件
返回res
获取二进制pdf文件列表后,我该怎么做才能在内存中生成一个压缩文件,并通过内容处理返回该文件作为响应?用于将pdf文件一次一个写入存档