问题
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