# Adding word document object variable inside a zip folder object

**URL:** <https://forum.djangoproject.com/t/adding-word-document-object-variable-inside-a-zip-folder-object/3084>\
**Category:** Using Django\
**Created:** [June 23, 2020, 6:57am UTC](https://forum.djangoproject.com/t/adding-word-document-object-variable-inside-a-zip-folder-object/3084 "2020-06-23T06:57:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierrejutard](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pierrejutard/32/8085_2.png) [@pierrejutard](https://forum.djangoproject.com/u/pierrejutard)\
**Post date:** [June 23, 2020, 6:57am UTC](https://forum.djangoproject.com/t/adding-word-document-object-variable-inside-a-zip-folder-object/3084/1 "2020-06-23T06:57:34Z")

</div>

Hello,

I create word documents on the fly and I would like to add them in a zip folder object (zipObj = ZipFile(‘Sample.zip’, ‘w’)) that will be downloadable in a web page.  
How do you add a word document object variable into a zip folder object in python (Note that the word document is not a word document that is already saved in my desktop but a document variable that come from the ‘docx’ module)?

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 23, 2020, 12:03pm UTC](https://forum.djangoproject.com/t/adding-word-document-object-variable-inside-a-zip-folder-object/3084/2 "2020-06-23T12:03:14Z")

</div>

Are you looking for the [writestr](https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.writestr) method on the ZipFile object?

---

<div class="post-metadata">

**Author:** ![pierrejutard](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/pierrejutard/32/8085_2.png) [@pierrejutard](https://forum.djangoproject.com/u/pierrejutard)\
**Post date:** [June 27, 2020, 3:14pm UTC](https://forum.djangoproject.com/t/adding-word-document-object-variable-inside-a-zip-folder-object/3084/3 "2020-06-27T15:14:22Z")

</div>

Hello Ken,

I did not use this method but the **write** method combine with an io.bytesIO stream to add word file( with their format) to a ZIP archive object:

f = io.BytesIO()  
zipObj = ZipFile(f, ‘a’)  
for File\_name in Obj\_Attr\_List:  
Contrat\_word = Create\_Word\_Contract(File\_name)  
Contrat\_word.save(File\_name + ‘.docx’)  
zipObj.write(File\_name + ‘.docx’)  
zipObj.close()  
response = HttpResponse(f.getvalue(),content\_type=‘application/zip’)  
response[‘Content-Disposition’] = ‘attachment; filename=ZipArchName.zip’  
return response

Thanks again for your help!  
PJ
