Traceback (most recent call last) when docx.Document() is used
Traceback (most recent call last) when docx.Document() is used
import docx
f = open('~/Desktop/python/test/draft.docx','rb')
document = docx.Document(f)
Traceback (most recent call last):
File "./test.py", line 56, in <module>
document = docx.Document(f)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/docx/api.py", line 25, in Document
document_part = Package.open(docx).main_document_part
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/docx/opc/package.py", line 116, in open
pkg_reader = PackageReader.from_file(pkg_file)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/docx/opc/pkgreader.py", line 32, in from_file
phys_reader = PhysPkgReader(pkg_file)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/docx/opc/phys_pkg.py", line 101, in __init__
self._zipf = ZipFile(pkg_file, 'r')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1200, in __init__
self._RealGetContents()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1267, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
Any help would be appreciated
Running python3.7 on OS X10.13
file draft.docx
1 Answer
1
Don't open the file before you pass it to Document()
. Just give it the path like you did in the open()
call above.
Document()
open()
It needs to be an actual Word .docx file. Note that you can just call document = Document()
to get started. The "save as" file name is provided in the document.save()
call. The file (if any) provided in the Document()
call is just the starting-point "template" to use.
document = Document()
document.save()
Document()
See the related documentation here:
https://python-docx.readthedocs.io/en/latest/user/documents.html
sorted out the issue. The draft.docx file was corrupted somehow. Changed the file and it worked. document = docx.Document('draft.docx') also worked.
– greybeardy
Sep 17 '18 at 14:17
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
Edit your Question and show us the output of
file draft.docx
in a console.– stovfl
Sep 16 '18 at 17:20