2017-09-21 4 views
0

私は現在以下のコードを使用して単語文書を生成し、cherrypyを使用してこれをWeb上で提供しています。ジップとサーバーの複数メモリ内ファイル

tpl.get_docx().save(iostream) 
cherrypy.response.headers['Content-Type'] = (
      'application/vnd.openxmlformats-officedocument' 
      '.wordprocessingml.document' 
      ) 
cherrypy.response.headers['Content-Disposition'] = (
      'attachment; filename={fname}.docx'.format(
       fname='SP' + kwargs['sp'] + '-'+ kwargs['WO'] + ' ' + kwargs['site'] + ' - ' + 'RPC Report' +'.docx' 
      ) 
      ) 
iostream.seek(0) 
return file_generator(iostream) 

さらにドキュメントを作成し、メモリで圧縮してWeb上で提供する予定です。どのようにこれがimplmentedことができる、私はzipfileライブラリを使用してみました、それは複雑なメモリ内のファイルを圧縮しているようです。

次の例ではGoogleの問題を解決するかもしれませんが、使用方法はわかりません。

import zipfile 
import StringIO 

zipped_file = StringIO.StringIO() 
with zipfile.ZipFile(zipped_file, 'w') as zip: 
    for i, file in enumerate(files): 
     file.seek(0) 
     zip.writestr("{}.csv".format(i), file.read()) 

zipped_file.seek(0) 

答えて

2

永続性の時間後、私はあなたがそれが答えマークしなければならないyeahhhh、

iostream = BytesIO() 
tpl.get_docx().save(iostream) 

iostream1 = BytesIO() 
tpl1.get_docx().save(iostream1) 

zip_output = StringIO.StringIO() 
file = zipfile.ZipFile(zip_output, "w") 
file.writestr("test.docx", iostream.getvalue()) 
file.writestr("test1.docx", iostream1.getvalue()) 
file.close() 

cherrypy.response.headers['Content-Type'] = 'application/zip' 
cherrypy.response.headers['Content-Disposition'] = 'attachment; filename="test.zip"' 

return zip_output.getvalue() 
+0

をこの作業を持って... – 576i

関連する問題