首页 > 解决方案 > 使用 PyExcel 写入 Excel 文件在 PyInstaller 捆绑的 exe (Windows) 上不起作用,写入空文件

问题描述

我使用 PyExcel 及其 ODS 和 XLSX 插件将数据写入 LibreOffice 和 Excel 工作表。该程序在 Python 解释器上执行时运行良好,但我发现当我从 PyInstaller 可执行文件执行程序时它不起作用(我在 Windows 10 上工作)。更具体地说,文件已创建,但它们似乎已损坏(0kB 文件格式无法识别),但在输出控制台或 PyInstaller 构建过程中均未显示任何错误消息。我设法创建了一个最小的示例来准确显示问题所在。

首先,它使用 Python 3.7.3、PyInstaller 3.4 和最后一个带有插件的 PyExcel 版本。此外,我需要 pywin32 库才能让 PyInstaller 正常工作。我在这里复制最小示例的代码,包括我用来构建 exe 文件的控制台脚本。如前所述,在 Python 解释器上完美运行,但构建的可执行文件只写入损坏的文件。

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

我不确定我是否遗漏了一些重要的隐藏导入或真正发生了什么。如果有人可以帮助我破解它,那将非常有用。提前致谢。

编辑:添加行

traceback.print_exc()

except块内,表明实际上引发了一个异常:

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

所以,它实际上并没有检测到插件,即使我指出隐藏的导入。我很困惑。

标签: pythonwindowspyinstallerpython-3.7pyexcel

解决方案


我只是通过明确导入代码上的插件来完成这项工作。

import pyexcel_xlsx
import pyexcel_ods

不过这看起来有点丑。。


推荐阅读