Python-Docx缺少默认模板

时间:2019-01-09 00:51:46

标签: python-3.x python-docx

我最近安装了python-docx软件包,但默认模板遇到了一些麻烦。我只是设置一个文档,并具有以下代码(在名为maintitle.py的文件中):

from docx import Document
from docx.shared import Inches

document = Document()

但是,出现以下错误:

Traceback (most recent call last):
  File "/Users/myname/Desktop/Python/maintitle.py", line 4, in <module>
    document = Document()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/api.py", line 25, in Document
    document_part = Package.open(docx).main_document_part
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/opc/package.py", line 128, in open
    pkg_reader = PackageReader.from_file(pkg_file)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/opc/pkgreader.py", line 32, in from_file
    phys_reader = PhysPkgReader(pkg_file)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/opc/phys_pkg.py", line 31, in __new__
    "Package not found at '%s'" % pkg_file
docx.opc.exceptions.PackageNotFoundError: Package not found at '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/templates/default-docx-template'

在目录“ /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/templates”中,只有四个文件:

default-footer.xml  default-settings.xml
default-header.xml  default-styles.xml

我用pip3 install python-docx安装了python-docx。我还应该做点其他事情吗?

1 个答案:

答案 0 :(得分:3)

v0.8.9版中存在一个问题,该问题在某些环境中引发了此错误。如果您安装v0.8.10,则该版本将消失。

$ pip install python-docx==0.8.10

在某些具有较旧setuptools版本的Ubuntu和Mint发行版中,您可能还需要更新setuptools

$ pip install -U setuptools

无论出于何种原因,这些发行版都有一个v20.x setuptools,其中最新的是v40.x。