我创建了一个函数,用Weasyprint创建多个pdf,将它们压缩在一起并下载zip文件。尝试用内部压缩程序解压缩Windows 10上的文件夹时,出现以下错误:
"An unexpected error is keeping you from copying the file. [...] Error 0x80070057" <
我可以跳过错误并提取文件。不过,在最好的情况下,我想防止这个错误。在
def get_all_shareholder_reports(request):
current_shareholders = list(models.objects.all())
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "a") as zip_file:
for shareholder in current_shareholders:
pdf_file_handle = io.BytesIO()
context_dict = get_report_details(pk=shareholder.shareholder_id)
html_string = render_to_string('template.html',
context_dict)
html_handler = HTML(string=html_string, base_url=request.build_absolute_uri())
html_handler.write_pdf(target=pdf_file_handle)
pdf_file_handle.seek(0)
pdf_string = pdf_file_handle.getvalue()
pdf_file_name ='Shareholder_Report_{}_{}_{}.pdf'.format(context_dict['shareholder'].forename,
context_dict['shareholder'].surname,
datetime.datetime.now().strftime(
"%d_%m_%Y_%H:%M:%S"))
zip_file.writestr(zinfo_or_arcname=pdf_file_name, data=pdf_string)
zip_buffer.seek(0)
response = HttpResponse(zip_buffer.getvalue(), content_type="application/x-zip-compressed")
response['Content-Disposition'] = 'attachment; filename=%s' % 'myzip.zip'
return response
我发现了:zip文件不喜欢文件名中的“:”。去掉它们就解决了这个问题。在
相关问题 更多 >
编程相关推荐