Python-Docx missing default template

一曲冷凌霜 提交于 2019-12-11 02:44:32

问题


I recently installed the python-docx package and I'm having some trouble with the default template. I'm just setting up a document and have the following code (in a file called maintitle.py):

from docx import Document
from docx.shared import Inches

document = Document()

However, I get the following error:

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'

Taking a look in the directory "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/docx/templates" there are only four files:

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

I installed python-docx with pip3 install python-docx. Should I do something else?


回答1:


There was a problem in release v0.8.9 that raised this error in certain environments. If you install v0.8.10 this should go away.

$ pip install python-docx==0.8.10

In some Ubuntu and possibly Mint distributions having an older setuptools version, you may also need to update setuptools:

$ pip install -U setuptools

For whatever reason, those distributions have a v20.x setuptools where the latest is v40.x.



来源:https://stackoverflow.com/questions/54101831/python-docx-missing-default-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!