1

I am trying to make an executable .exe using pyinstaller on python code that is using the package docxtpl as well as homemade docx templates.

I am using Windows 10 together with conda 4.8.2 and python 3.7.6

The context is a program that produces automatically reports by filling a docx template. The program is working well when it is not an executable and I also manage to produce an executable. The problem occurs when I execute my executable.

I get this error message:

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

Where as in the spec file I used the following data:

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

Inside the program:

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))

I would be very grateful if somebody could tell me how I can integrate my docx template into the executable so that it works.

Apparently somebody has had the same problem here, but I found no satisfying solution: https://github.com/elapouya/python-docx-template/issues/35

Matt Dnv
  • 1,620
  • 13
  • 23
  • 1
    Change the data destination to `.`, not `ReportTemplate.docx` – Legorooj Mar 07 '20 at 23:58
  • @Legorooj thank you for your comment. However, I have also tried: datas=[('C:\\Users\\username\\eclipse-workspace\\different_stuff\\allmytools\\mytool\\src\\report_template\\ReportTemplate.docx','.')] but the result was identical – Matt Dnv Mar 08 '20 at 09:46
  • Try without --onefile if you haven't already, with the data option I suggested, and see if the docx file is inside dist or not. – Legorooj Mar 08 '20 at 22:27
  • @Legorooj I have also tried this, it doesn't work either. In addition to this I have also added the __init__.py files inside the data folders – Matt Dnv Mar 09 '20 at 09:02
  • One of the turnarounds would be to put into a python file the content of : docxtpl.DocxTemplate(join(self.template_dir, self.template_name)) but it is quite ugly and unmaintainable and I will try this solution at last resort – Matt Dnv Mar 09 '20 at 09:08

1 Answers1

1

Ok I have solved my problem: in the spec file, the data list was wrongly defined. First and foremost, my spec file is inside the folder:

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

Thus in my spec file, I have written instead:

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

where the second part of the tuple says in which part of the temporary folder created by the exe the file is to be found, and it also says what and where to copy the file in the structure inside the exe binary

Matt Dnv
  • 1,620
  • 13
  • 23
  • @Legorooj I found the answer – Matt Dnv Mar 09 '20 at 15:21
  • 1
    I was sure you had invalid paths somewhere; that's the only cause of this issue. Please select the answer as correct with the tick button below the voting buttons – Legorooj Mar 09 '20 at 23:19
  • @Legorooj , i set a 50 bounty on that other pyinstaller problem https://stackoverflow.com/questions/60655910/use-pyunpack-inside-an-executable-file-made-with-pyinstaller-in-combination-with – Matt Dnv Mar 14 '20 at 21:43