一开始想把包含openpyxl库的python程序用pyinstaller打包成exe文件,程序需要用户输入:
# -*- coding: utf-8 -*-
from openpyxl import Workbook
wb = Workbook()
# grab the active worksheet
ws = wb.active
number=input("please input number:")
# Data can be assigned directly to cells
ws['A1'] = number
# Rows can also be appended
ws.append([1, 2, 3])
# Save the file
wb.save("sample.xlsx")
使用pyinstaller时
pyinstaller -F -c excel_test.py
报错了 但报错的那个黑框很短时间就自动关闭了 要想查看为什么错了 可以把pyinstaller参数中的-c 换成-w 也就是在运行程序时不需要那个黑框(注意:换成-w时候就不能用户输入了,需要先把代码里的input的值写死)
即
pyinstaller -F -w excel_test.py
就可以运行exe看到错误的原因了
显示
Traceback (most recent call last):
File "excel_test.py", line 8, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "openpyxl\__init__.py", line 6, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "openpyxl\workbook\__init__.py", line 4, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "openpyxl\workbook\workbook.py", line 9, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "openpyxl\worksheet\_write_only.py", line 13, in <module>
File "openpyxl\worksheet\_writer.py", line 23, in init openpyxl.worksheet._writer
ModuleNotFoundError: No module named 'openpyxl.cell._writer'
然后在网上查了 需要使用 pyinstaller -F -c --hidden-import=openpyxl excel_test.py
但还是报相同的错
解决方法:
使用
pyinstaller -F -c --hidden-import=openpyxl.cell._writer excel_test.py
引入具体的openpyxl.cell._writer模块即可解决
同理 如果之后报错显示缺少win32timezone 那么在程序中import引入win32timezone即可运行