我最近安装了 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。我应该做点别的吗?
v0.8.9 版本中存在一个问题,在某些环境下会引发此错误。如果您安装 v0.8.10,这个问题就会消失。
$ pip install python-docx==0.8.10
在某些 Ubuntu 和可能的 Mint 发行版中,安装工具版本较旧,您可能还需要更新
setuptools
:
$ pip install -U setuptools
无论出于何种原因,这些发行版都有 v20.x 安装工具,其中最新的是 v40.x。
我有同样的问题,但是使用以下命令后一切看起来都很好。
用途:
pyinstaller --collect-data "docxcompose" --onefile d:\python\Merge_Docx.py
而不是:
pyinstaller --onefile d:\python\Merge_Docx.py
(d:\python\Merge_Docx.py是位于的python脚本)
解决 python-docx==1.1.2 中的问题,
只需按照以下步骤操作:
找到default_sanitized.docx 文件:
C:\Users
复制并重命名文件: • 制作default_sanitized.docx 的副本。 • 将复制的文件重命名为default.docx。