小编P. *_*ino的帖子

解压文件时出现“错误0x80070057:参数不正确”

我创建了一个函数来使用Wea​​syprint创建多个 PDF ,将它们压缩在一起并下载 zip 文件。当尝试使用内部 zip 程序在 Windows 10 上提取文件夹时,我收到此错误:

“意外错误使您无法复制文件。[...]错误 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
Run Code Online (Sandbox Code Playgroud)

python django zip weasyprint

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

django ×1

python ×1

weasyprint ×1

zip ×1