如何将pyinstaller与docxtpl一起使用?

时间:2020-03-06 16:57:59

标签: python templates jinja2 pyinstaller docx

我正在尝试使用pyinstaller在使用软件包docxtpl以及自制docx模板的python代码上创建可执行文件.exe。

我正在将Windows 10与conda 4.8.2和python 3.7.6一起使用

上下文是通过填充docx模板自动生成报告的程序。该程序不是可执行文件时运行良好,我也设法生成了可执行文件。当我执行我的可执行文件时,会发生问题。

我收到此错误消息:

docx.opc.exceptions.PackageNotFoundError: Package not found at
C:\Users\username\AppData\Local\Temp\_MEI100562\mytool\src\report_template\ReportTemplate.docx

在规范文件中,我使用了以下数据:

datas=[('C:\\Users\\username\\eclipse-workspace\\different_stuff\\allmytools\\mytool\\src\\report_template','ReportTemplate.docx')]

程序内部:

from docxtpl import DocxTemplate, InlineImage
from docx.shared import Mm
[...]
        self.template_dir = join(dirname(dirname(__file__)), 'report_template')
        self.template_name = "ReportTemplate.docx"
        self.doc = DocxTemplate(join(self.template_dir, self.template_name))

如果有人能告诉我如何将docx模板集成到可执行文件中以使其起作用,我将不胜感激。

显然有人在这里遇到了同样的问题,但是我没有找到令人满意的解决方案: https://github.com/elapouya/python-docx-template/issues/35

1 个答案:

答案 0 :(得分:1)

好的,我已经解决了我的问题:在spec文件中,数据列表定义有误。 首先,我的spec文件位于以下文件夹中:

'C:\\Users\\username\\eclipse-workspace\\different_stuff\\allmytools\\mytool\\'

因此,在我的规格文件中,我写了:

datas=[('./src/report_template/*','mytool/src/report_template')] 

元组的第二部分表示要在exe创建的临时文件夹的哪个部分中找到文件,还表示要在exe二进制文件内部的结构中复制文件的内容和位置