ersvoid
ersvoid

Reputation: 1

How to create a word document using a python tkinter executable?

I am trying to create a python tkinter executable that displays information from an Oracle database and can also export that information into a MS Word (.docx) document. It also does the same with Excel.

Currently, whenever I run the script through PyCharm, I am able to create a new Word document and a new Excel spreadsheet; however, after using pyinstaller to create an executable, I cannot export a new Word document, but I am able to export a new Excel spreadsheet.

I think I either need to find some unknown Word module that actually creates Word docs the same way xlsxwriter creates Excel sheets; or I need to somehow bundle a Word file into the executable.

Python 3.7, PyCharm 2019.1.1 Community Edition. Modules - tkinter, python-docx, docx-mailmerge, xlsxwriter, pyinstaller

I was using docx-mailmerge and was merging the data into a Word document template, but then I tried using python-docx to try and "create" my own Word document. (From the documentation for python-docx: "Actually, it only lets you make changes to existing documents; it’s just that if you start with a document that doesn’t have any content, it might feel at first like you’re creating one from scratch.")

I have tried using pyinstaller to create a folder distribution and single-file executable. I think my best option is to create a folder and then manually add a Word template into some subfolder, but I have no idea which. If this is the case, I believe I can use either Word module.

I can export Excel spreadsheets using xlsxwriter, no problem.

I added a try/except statement to catch the error (and added a label to the app that would display the error name) and I went through the entire list of exceptions in the python doc, but only the catch-all "except:" would catch.

def data_pull(user, pw, _id):
    # connects to oracle and pulls data into a list

class App(Frame):
    # a login frame not shown
    # self.create_widgets()

    def create_widgets(self):
        # various StringVar() variables
        # oracle data entry query (=_id)
        # Button that calls entry query
        # frames and scrolledtext for displaying the data
        # Button to export to word
        # Button to export to excel

     def word_export(self, lst, path):
        # export using python-docx

     def excel_export(self, lst, path):
        # export using xlsxwriter

     def entry_callback(self):
        # a = data_pull(user, pw, _id)
        # parse a into a long string
        # pass string to scrolledtext widget

When this is ran from inside PyCharm, the Word document is created no problem, but when this is ran from the executable, nothing happens.

Upvotes: 0

Views: 1596

Answers (1)

Masoud Rahimi
Masoud Rahimi

Reputation: 6031

As already mentioned in the comments the problem is when you try to use python-docx, it has some template files that actually not related to python, so PyInstaller won't recognize them and thus they would be missed in final executable.

A simple way in these situations is just embedding the whole dependence directory. You need to use Tree class and add the docx's templates path (remember to replace ./env/Lib/site-packages/docx/templates with a proper path on your system):

a = Analysis(
...
a.datas += Tree("./env/Lib/site-packages/docx/templates", prefix='docx/templates')
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
...

Upvotes: 1

Related Questions