Python 如何将pyinstaller与docxtpl一起使用?

Python 如何将pyinstaller与docxtpl一起使用?,python,templates,jinja2,pyinstaller,docx,Python,Templates,Jinja2,Pyinstaller,Docx,我试图在python代码上使用pyinstaller生成一个可执行的.exe,该代码使用包docxtpl以及自制的docx模板 我正在使用Windows10以及conda 4.8.2和python 3.7.6 上下文是一个通过填充docx模板自动生成报告的程序。当程序不是可执行文件时,它运行良好,我还设法生成了一个可执行文件。当我执行可执行文件时,问题就出现了 我收到以下错误消息: docx.opc.exceptions.PackageNotFoundError: Package not fou

我试图在python代码上使用pyinstaller生成一个可执行的.exe,该代码使用包docxtpl以及自制的docx模板

我正在使用Windows10以及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
其中,在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模板集成到可执行文件中,使其工作,我将不胜感激

显然有人在这里遇到了同样的问题,但我没有找到令人满意的解决方案:

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

'C:\\Users\\username\\eclipse-workspace\\different_stuff\\allmytools\\mytool\\'
因此,在我的规范文件中,我写了:

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

其中,元组的第二部分说明在exe创建的临时文件夹的哪个部分可以找到文件,并且还说明在exe二进制文件内部的结构中复制文件的内容和位置

将数据目标更改为
,而不是
ReportTemplate.docx
@Legoroj谢谢您的评论。但是,我也尝试过:datas=[('C:\\Users\\username\\eclipse workspace\\distribute\u stuff\\allmytools\\mytool\\src\\report\u template\\ReportTemplate.docx','.')]但是结果是identicalTry,没有--onefile,如果还没有,使用我建议的数据选项,然后看看docx文件是否在dist内。@legoroj我也试过这个,它也不起作用。除此之外,我还在数据文件夹中添加了init.py文件,其中一个改进是将以下内容放入python文件中:docxtpl.DocxTemplate(join(self.template_dir,self.template_name))但这是相当丑陋和无法维护的,我将尝试这个解决方案在最后resort@Legorooj我找到了答案我确信你在某处有无效路径;这是这个问题的唯一原因。请用投票下方的勾选按钮选择正确答案buttons@Legorooj,我在另一个问题上悬赏50英镑