我收到了一个包含以下代码的ipython notebook文件,我正尝试通过Jupyter执行它。
import docx;
from django.utils.encoding import smart_text;
doc = docx.Document('test_file.docx')我得到了下面的回溯
---------------------------------------------------------------------------
PackageNotFoundError Traceback (most recent call last)
<ipython-input-8-2ff3b55810a7> in <module>()
1 import docx;
2 from django.utils.encoding import smart_text;
----> 3 doc = docx.Document('test_file.docx')
4 statements = [para.text.strip() for para in doc.paragraphs
5 if para.text.strip() != '']
C:\Program Files (x86)\Anaconda2\lib\site-packages\docx\api.pyc in Document(docx)
23 """
24 docx = _default_docx_path() if docx is None else docx
---> 25 document_part = Package.open(docx).main_document_part
26 if document_part.content_type != CT.WML_DOCUMENT_MAIN:
27 tmpl = "file '%s' is not a Word file, content type is '%s'"
C:\Program Files (x86)\Anaconda2\lib\site-packages\docx\opc\package.pyc in open(cls, pkg_file)
114 *pkg_file*.
115 """
--> 116 pkg_reader = PackageReader.from_file(pkg_file)
117 package = cls()
118 Unmarshaller.unmarshal(pkg_reader, package, PartFactory)
C:\Program Files (x86)\Anaconda2\lib\site-packages\docx\opc\pkgreader.pyc in from_file(pkg_file)
30 Return a |PackageReader| instance loaded with contents of *pkg_file*.
31 """
---> 32 phys_reader = PhysPkgReader(pkg_file)
33 content_types = _ContentTypeMap.from_xml(phys_reader.content_types_xml)
34 pkg_srels = PackageReader._srels_for(phys_reader, PACKAGE_URI)
C:\Program Files (x86)\Anaconda2\lib\site-packages\docx\opc\phys_pkg.pyc in __new__(cls, pkg_file)
29 else:
30 raise PackageNotFoundError(
---> 31 "Package not found at '%s'" % pkg_file
32 )
33 else: # assume it's a stream and pass it to Zip reader to sort out
PackageNotFoundError: Package not found at 'test_file.docx'我最近卸载了docx并重新安装了python-docx,我想知道这是否相关,或者这是一个单独的问题?
发布于 2017-05-12 09:54:35
这意味着指定的文件('test_file.docx')不存在或不是Word文档。
检查路径是否正确,以及是否可以使用Word (或LibreOffice等)打开文档。
您可能缺少文件名的路径部分。有时,要知道Python认为缺省目录是什么是很棘手的,缺省目录是Python查找没有路径的文件名的地方。
https://stackoverflow.com/questions/43920427
复制相似问题