我正在尝试使用python代码上的pyinstaller来制作一个可执行的.exe,该代码使用了docxtpl包以及自制的docx模板。
我使用的是Windows 10、conda 4.8.2和python 3.7.6
context是一个通过填充docx模板自动生成报告的程序。当程序不是可执行文件时,它工作得很好,而我也设法生成了一个可执行文件。当我执行我的可执行文件时,问题出现了。
我收到这个错误消息:
docx.opc.exceptions.PackageNotFoundError: Package not found at
C:\Users\username\AppData\Local\Temp\_MEI100562\mytool\src\report_template\ReportTemplate.docx在spec文件中,我使用了以下数据:
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
发布于 2020-03-09 23:21:23
好了,我已经解决了我的问题:在spec文件中,数据列表被错误地定义。首先也是最重要的,我的spec文件在文件夹中:
'C:\\Users\\username\\eclipse-workspace\\different_stuff\\allmytools\\mytool\\'因此,在我的规范文件中,我改为这样写:
datas=[('./src/report_template/*','mytool/src/report_template')] 其中,元组的第二部分表示要在由exe创建的临时文件夹的哪一部分中找到该文件,它还表示在exe二进制文件内的结构中复制该文件的内容和位置。
https://stackoverflow.com/questions/60568502
复制相似问题