这是我下载包含 .docx 文件的 zip 文件的代码,
def reportsdlserien(request):
selected_sem = request.POST.get("semester","SS 2016")
docx_title="Report_in_%s.docx" % selected_sem.replace(' ','_')
document = Document()
f = io.BytesIO()
zip_title="Archive_in_%s.zip" % selected_sem.replace(' ','_')
zip_arch = ZipFile( f, 'a' )
document.add_heading("Report in "+selected_sem, 0)
document.add_paragraph(date.today().strftime('%d %B %Y'))
document.save(docx_title)
zip_arch.write(docx_title)
zip_arch.close()
response = HttpResponse(
f.getvalue(),
content_type='application/zip'
)
response['Content-Disposition'] = 'attachment; filename=' + zip_title
return response
唯一的问题是,它还创建了我不需要的 .docx 文件。我也想将 BytesIO 用于 docx 文件,但我无法将其添加到存档中,该命令zip_arch.write(BytesIOdocxfile)
不起作用。是否有另一个命令来执行此操作?谢谢!