You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you create a new Microsoft Word document (without opening or editing it), and try to load it using python-docx with the following code:
fromdocximportDocumentf=open('New Microsoft Word Document.docx', 'rb')
document=Document(f)
f.close()
You’ll get this error:
<_io.BufferedReader name='New Microsoft Word Document.docx'>
Traceback (most recent call last):
...
zipfile.BadZipFile: File is not a zip file
This happens because a brand-new Word document that hasn't been opened or edited yet isn't actually a valid .docx file—it’s just a placeholder and not a proper ZIP archive (which .docx files are under the hood). So python-docx can’t read it and throws a BadZipFile error.
Regards...
The text was updated successfully, but these errors were encountered:
@dp-006 You can't create a DOCX file from scratch with python-docx. All you can do is open an existing DOCX and edit it.
When you say document = Document(), that's loading a "default-template.docx" file that's included with the package. Otherwise what you give to Document needs to be a valid path (or file-like object) that is already a DOCX file.
This code is how you do what I think you're trying to do in the code above:
document=Document()
document.save("New Microsoft Word Document.docx")
I just wanted to share something interesting:
When you create a new Microsoft Word document (without opening or editing it), and try to load it using
python-docx
with the following code:You’ll get this error:
This happens because a brand-new Word document that hasn't been opened or edited yet isn't actually a valid
.docx
file—it’s just a placeholder and not a proper ZIP archive (which.docx
files are under the hood). Sopython-docx
can’t read it and throws aBadZipFile
error.Regards...
The text was updated successfully, but these errors were encountered: