Python docx模块在创建.exe时给出AssertionError

时间:2014-02-10 11:50:45

标签: python py2exe assertion python-docx

我有一个标题为my_python_file.py的Python文件,其中包含使用.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写得很好。

如何让{{1}}模块正常工作?在制作exe时我是否还要添加任何其他路径/变量?

提前致谢

2 个答案:

答案 0 :(得分:0)

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

更改此内容:

_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

答案 1 :(得分:0)

我没有更改某些库文件,而是发现明确告诉python-docx在哪里查找模板更简单,更清晰,即:

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

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