使用docx.Document()时的回溯(最近调用最后一次)

问题描述 投票:0回答:1
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
  1. 卸载了docx并安装了python-docx
  2. 卸载lxml也没有任何效果。

任何帮助将不胜感激在OS X10.13上运行python3.7

python python-3.x docx python-docx
1个回答
0
投票

在将文件传递给Document()之前不要打开文件。就像你在上面的open()电话中所做的那样给它一条路径。

它需要是一个真正的Word .docx文件。请注意,您只需致电document = Document()即可开始使用。 document.save()调用中提供了“另存为”文件名。 Document()调用中提供的文件(如果有)只是要使用的起点“模板”。

请参阅此处的相关文档: https://python-docx.readthedocs.io/en/latest/user/documents.html

© www.soinside.com 2019 - 2024. All rights reserved.