Bouke / docx-mailmerge

Mail merge for Office Open XML (docx) files without the need for Microsoft Office Word.
MIT License
273 stars 104 forks source link

AttributeError: 'NoneType' object has no attribute 'filelist' #85

Open leonatez opened 4 years ago

leonatez commented 4 years ago

I'm new to this module. I tried exactly your example but it says below exception:

Traceback (most recent call last): File "/Users/mac/Downloads/BBEditRunTemp-test.py", line 6, in document.write('output.docx') File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mailmerge.py", line 121, in write for zi in self.zip.filelist: AttributeError: 'NoneType' object has no attribute 'filelist'

The code is here: from mailmerge import MailMerge with MailMerge('input.docx') as document: ... document.merge(field1='docx Mail Merge', field2='Can be used for merging docx documents') document.write('output.docx')

ankit311 commented 2 years ago

Instead of using with to open the existing document, use MailMerge function to read the file object. To fix the error use the below code:

from mailmerge import MailMerge document = MailMerge('input.docx') document.merge(field1='docx Mail Merge', field2='Can be used for merging docx documents') document.write('output.docx')