Gabriel Pena
Gabriel Pena

Reputation: 439

Writing to Excel files with PyExcel not working on PyInstaller bundled exe (Windows), writes empty-corrupt files

I was using PyExcel with its ODS and XLSX plugins to write data to LibreOffice and Excel sheets. The program worked perfectly fine when executed on the Python interpreter, but I just found it's not working when I execute the program from a PyInstaller executable (I'm working on Windows 10). To be more specific, the files are created but they seem to be corrupt (0kB file with unrecognized format), but no error messages are shown neither on the output console or the PyInstaller build process. I managed to create a minimal example that shows exactly what the problem is.

To begin with, this uses Python 3.7.3, PyInstaller 3.4, and the last PyExcel version with its plugins. Also, I needed pywin32 library in order to get PyInstaller working. I'm copying here the code of the minimal example, including the console script I used to build the exe file. As told, works perfectly on the Python interpreter, but the built executable only writes corrupt files.

-- open_dialog.py --

from tkinter import filedialog


def open_dialog():
    file = filedialog.asksaveasfile(mode='wb',
                                       title='Export',
                                       defaultextension='xlsx',
                                       filetypes=([("XLSX", ".xlsx"),
                                         ("ODS", ".ods"),
                                         ("All files", ".*")])
                                       )
    if file is None:
        raise Exception()

    return file

-- main.py --

from collections import OrderedDict

import pyexcel

from open_dialog import open_dialog

try:
    with open_dialog() as file:

        sheet_1 = []
        sheet_1.append(['Sheet 1'])
        sheet_1.append([''])

        sheet_2 = []
        sheet_2.append(['Sheet 2'])
        sheet_2.append([''])

        book = OrderedDict()
        book["Sheet 1"] = sheet_1
        book["Sheet 2"] = sheet_2
        book_excel = pyexcel.get_book(bookdict=book)

        book_excel.save_as(file.name)

except Exception:
    print("Failed to write file")

-- build_windows.bat --

set PYTHONPATH=.
PyInstaller "main.py" ^
--name "test" ^
--clean ^
--distpath "exe" ^
--hidden-import pyexcel ^
--hidden-import pyexcel.plugins.renderers.excel ^
--hidden-import pyexcel.plugins.renderers._texttable ^
--hidden-import pyexcel.plugins.parsers.excel ^
--hidden-import pyexcel.plugins.sources.http ^
--hidden-import pyexcel.plugins.sources.file_input ^
--hidden-import pyexcel.plugins.sources.memory_input ^
--hidden-import pyexcel.plugins.sources.file_output ^
--hidden-import pyexcel.plugins.sources.output_to_memory ^
--hidden-import pyexcel.plugins.sources.pydata.bookdict ^
--hidden-import pyexcel.plugins.sources.pydata.dictsource ^
--hidden-import pyexcel.plugins.sources.pydata.arraysource ^
--hidden-import pyexcel.plugins.sources.pydata.records ^
--hidden-import pyexcel_io ^
--hidden-import pyexcel_io.readers ^
--hidden-import pyexcel_io.writers ^
--hidden-import pyexcel_io.database ^
--hidden-import pyexcel_io ^
--hidden-import pyexcel_ods ^
--hidden-import pyexcel_ods.odsw ^
--hidden-import pyexcel_xlsx ^
--hidden-import pyexcel_xlsx.xlsxw ^

I'm not sure if I'm missing some important hidden import or what is really going on. It'd be really useful if someone can help me crack this. Thanks in advance.

EDIT: adding the line

traceback.print_exc()

inside the except block, showed that there's actually an exception being raised:

pyexcel_io.exceptions.SupportingPluginAvailableButNotInstalled: Please install one of these plugins for write data in 'xlsx': pyexcel-xlsx,pyexcel-xlsxw

So, it's actually not detecting the plugins, even though I am indicating the hidden imports. I'm pretty confused.

Upvotes: 0

Views: 1508

Answers (1)

Gabriel Pena
Gabriel Pena

Reputation: 439

I got this working simply by EXPLICITLY importing the plugins on the code.

import pyexcel_xlsx
import pyexcel_ods

This seems kinda ugly though..

Upvotes: 1

Related Questions