python - How to add a temporary .docx file to a zip archive in django -



python - How to add a temporary .docx file to a zip archive in django -

here code downloading zip file, containing .docx file,

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 homecoming response

the problem is, creates .docx file, dont need. wanted utilize bytesio docx file too, cant add together archive, command zip_arch.write(bytesiodocxfile) doesn't work. there command this? give thanks you!

use writestr() function add together bytes archive:

data = stringio() document.save(data) # or library requires this. zip_arch.writestr(docx_title, bytes(data.getvalue()))

i've done stringio, don't see why bytesio would't work well.

python django zipfile python-docx

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -