0

I have made a program that is creating a docx file based on user input I get from a GUI using pysimplegui.

I wish to have another docx file append to the one created from the user interface, but my exe file is failing to execute.

It works when i run the program in PyCharm, but my exe file fails. I suspect that there is some path error, but I have tried almost all possible combinations that could be the error so now i suspect it might have something to do with python-docxcompose.

My text_file_db contains lots av txt files I am able to access in the exe file, but when I try to use python-docxcompose functions it fails.

Directory overview

src/
    beta.py
    directory.py
    doc_write.py
    text_file_db/
        docs/
            document.docx

My code:

#! c:/user/src/beta.py


def beta():

    values = gui()
    create_doc(values)

    return 

With help from Max's answer from (Bundling data files with PyInstaller (--onefile))


#! c:/user/src/doc_write.py

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except AttributeError:
        base_path = os.path.abspath(".")
    path = os.path.join(base_path, relative_path)
    return path


def create_doc(values):
    doc = Document()
    relative_path = os.path.join('text_file_db\\docs','document.docx'
    path = resource_path(relative_path)
    doc = combin_docs(doc, path)
...

#! c:/user/src/directory.py

from docxcompose.composer import Composer
from docx import Document as Document_compose

def combin_docs(doc, appending):
    alt_doc = Composer(doc)
    doc2 = Document_compose(appending)
    alt_doc.append(doc2)
    new_doc = alt_doc.doc
    return new_doc

To create the exe I use pyinstaller.


$ pyinstaller --windowed --noconsole --clean --onefile beta.py

#! c:/user/src/beta.spec

Adding:

exe = EXE(pyz,
          a.scripts,
          a.binaries,
          Tree('./text_file_db', prefix='text_file_db'),
....


$ pyinstaller --windowed --noconsole --clean --onefile beta.spec

´´´
  • I am having the same issue. At the moment, after debugging using --onedir, I see the exe is failing because the docxcompose library/folder is not importing during packaging. – JcoOnline Jan 14 '20 at 21:40

2 Answers2

1

I found a related solution that can potentially help your problem. See my answer to manually including docxcompose as a package via the spec file at the following thread: How can I add a Python site-package folder (that's not being included) to a PyInstaller spec file?

JcoOnline
  • 243
  • 2
  • 13
1

Try to use below command:

pyinstaller --collect-data "docxcompose" --onefile d:\python\Merge_Docx.py

(d:\python\Merge_Docx.py is where python script located)

Sagar Darekar
  • 982
  • 9
  • 14
YC Chan
  • 51
  • 1