【问题标题】:How can I use pyinstaller together with docxtpl?如何将 pyinstaller 与 docxtpl 一起使用?
【发布时间】: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


【解决方案1】:

好的,我已经解决了我的问题:在规范文件中,数据列表定义错误。 首先,我的规范文件在文件夹中:

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

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

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

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

【讨论】:

  • @Legorooj 我找到了答案
  • 我确定您在某处有无效路径;这是这个问题的唯一原因。请用投票按钮下方的勾号按钮选择正确的答案
  • @Legorooj,我为其他 pyinstaller 问题设置了 50 赏金stackoverflow.com/questions/60655910/…
猜你喜欢
  • 2016-12-21
  • 2021-05-17
  • 2018-05-23
  • 2020-02-21
  • 2018-07-22
  • 2019-08-06
  • 2013-02-01
  • 2018-05-04
  • 2018-05-16
相关资源
最近更新 更多