【发布时间】:2020-03-06 16:57:59
【问题描述】:
我正在尝试在使用包 docxtpl 以及自制 docx 模板的 python 代码上使用 pyinstaller 制作一个可执行的 .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
【问题讨论】:
-
将数据目标更改为
.,而不是ReportTemplate.docx -
@Legorooj 感谢您的评论。但是,我也尝试过: datas=[('C:\\Users\\username\\eclipse-workspace\\different_stuff\\allmytools\\mytool\\src\\report_template\\ReportTemplate.docx','.' )] 但结果是相同的
-
尝试不使用 --onefile 如果你还没有,使用我建议的数据选项,看看 docx 文件是否在 dist 中。
-
@Legorooj 我也试过这个,它也不起作用。除此之外,我还在数据文件夹中添加了 init.py 文件
-
其中一个变通方法是将以下内容放入 python 文件中: docxtpl.DocxTemplate(join(self.template_dir, self.template_name)) 但它非常丑陋且不可维护,我会尝试这个解决方案是万不得已
标签: python templates jinja2 pyinstaller docx