多个 openpyxl xlsx 工作簿到一个 .zip 文件中以供下载
multiple openpyxl xlsx workbooks into one .zip file for download
我正在尝试从表单中获取一些 xlsx 文件,我使用 openpyxl 加载它们并进行一些数据处理..最后我需要下载所有已处理的压缩 xlsx 文件给用户。
这是我到目前为止所做的一个例子
if form.is_valid():
s = StringIO.StringIO()
zf = zipfile.ZipFile(s, mode="w")
for xlsx in request.FILES.getlist('xlsxs'):
element_column = "G"
element_row = 16
massar_column = "C"
massar_row_start = 18
loop = column_index_from_string(element_column)
while (loop <= ws.max_column):
for i in range(massar_row_start, ws.max_row+1):
# ...
ws["%s%s" % (element_column,i)] = 0
# ...
loop+=2
element_column = get_column_letter(loop)
buf = save_virtual_workbook(wb)
zf.write(buf) # or zf.write(wb)
zf.close()
response = HttpResponse(s.getvalue(), content_type="application/x-zip-compressed")
response['Content-Disposition'] = "attachment; filename=notes.zip"
return response
我收到错误
TypeError at My_view
stat() argument 1 must be encoded string without null bytes, not str
在此先感谢您提供的任何帮助。
save_virtual_workbook
returns 字节流 - source.
您正在将此值传递给需要文件名的 ZipFile.write。
我认为您应该使用 ZipFile.writestr,并且您需要提供将在存档中使用的文件名。我不确定您是如何收到错误消息的,但这是我看到的第一个错误。
我正在尝试从表单中获取一些 xlsx 文件,我使用 openpyxl 加载它们并进行一些数据处理..最后我需要下载所有已处理的压缩 xlsx 文件给用户。
这是我到目前为止所做的一个例子
if form.is_valid():
s = StringIO.StringIO()
zf = zipfile.ZipFile(s, mode="w")
for xlsx in request.FILES.getlist('xlsxs'):
element_column = "G"
element_row = 16
massar_column = "C"
massar_row_start = 18
loop = column_index_from_string(element_column)
while (loop <= ws.max_column):
for i in range(massar_row_start, ws.max_row+1):
# ...
ws["%s%s" % (element_column,i)] = 0
# ...
loop+=2
element_column = get_column_letter(loop)
buf = save_virtual_workbook(wb)
zf.write(buf) # or zf.write(wb)
zf.close()
response = HttpResponse(s.getvalue(), content_type="application/x-zip-compressed")
response['Content-Disposition'] = "attachment; filename=notes.zip"
return response
我收到错误
TypeError at My_view
stat() argument 1 must be encoded string without null bytes, not str
在此先感谢您提供的任何帮助。
save_virtual_workbook
returns 字节流 - source.
您正在将此值传递给需要文件名的 ZipFile.write。
我认为您应该使用 ZipFile.writestr,并且您需要提供将在存档中使用的文件名。我不确定您是如何收到错误消息的,但这是我看到的第一个错误。