0

我有一个名为的 Python 文件my_python_file.py,其中包括.doc使用该python-docx模块的文件。完美创建并且.doc没有问题。当我构建.exe我的脚本并尝试制作.doc. 出现AssertionError问题。

这是我的 exe 制造商代码(exe_maker.py):

from distutils.core import setup
import py2exe, sys, os
sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 3, 'compressed': True, 'includes': ['lxml.etree', 'lxml._elementpath', 'gzip', 'docx']}},
    windows = [{'script': "my_python_file.py"}],
    zipfile = None,
)

似乎将 python 脚本移动到不同的位置会产生错误。

  File "docx.pyc", line 1063, in savedocx
AssertionError

这是savedocx行:

document = newdocument()
[...]
coreprops = coreproperties(title=title, subject=subject, creator=creator, keywords=keywords)
approps = appproperties()
contenttypes2 = contenttypes()
websettings2 = websettings()
wordrelationships2 = wordrelationships(relationships)
path_save = "C:\output"
savedocx(document, coreprops, approps, contenttypes2, websettings2, wordrelationships2, path_save)

savedox写得很好,因为它不是文件时可以工作.exe

如何使docx模块正常工作?制作 exe 时是否需要添加任何其他路径/变量?

提前致谢

4

2 回答 2

0

api.py我通过编辑位于系统 Python 文件夹中的 docx egg 文件夹的文件解决了这个问题。

改变这个:

_thisdir = os.path.split(__file__)[0]
_default_docx_path = os.path.join(_thisdir, 'templates', 'default.docx')

对此:

thisdir = os.getcwd()
_default_docx_path = os.path.join(thisdir, 'templates', 'default.docx')

第一个是获取实际运行的程序并将其添加到找到templates文件夹的路径中。
C:\myfiles\myprogram.exe\templates\default.docx

该解决方案仅采用路径,而不采用正在运行的程序。
C:\myfiles\templates\default.docx

于 2014-02-17T10:35:12.580 回答
0

与其更改一些库文件,我发现明确告诉 python-docx 在哪里查找模板更容易和更清晰,即:

document = Document('whatever/path/you/choose/to/some.docx')

这有效地解决了 py2exe 和 docx 路径问题。

于 2014-07-23T00:57:48.563 回答