参考链接:
https://stackoverflow.com/questions/35642322/pyinstaller-and-python-docx-module-do-not-work-together
在自己写PyQt5+python docx+Python3.7 打包exe的时候出现问题,发现点击按钮有闪退现象,最后发现是pyinstaller打包的时候没有把docx模块打包进去,参考上述链接但是有所不同的是变化如下:
首先生成**.spec**文件 在脚本路径下运行 :pyi-makespec your_py_file_name.py
在记事本中打开.spec文件并通过添加以下内容进行编辑:
# -*- mode: python -*-
import sys
from os import path
site_packages = next(p for p in sys.path if 'site-packages' in p)
##原来的代码是找的库'C:\\Users\\sufor\\AppData\\Roaming\\Python\\Python37\\site-packages'这个库中没有docx
site_packages = 'C:\Users\sufor\AppData\Local\Programs\Python\Python37\Lib\site-packages'
block_cipher = None
a = Analysis(['xml_reader.py'],
pathex=['C:\\Users\\Lenovo\\Desktop\\exe'],
binaries=[],
datas=[(path.join(site_packages,"docx","templates"),
"docx/templates")], #原来datas替换为现在datas
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='xml_reader',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='xml_reader')
最后运行在当前脚步路径下运行cmd命令:pyinstaller your_py_file_name.spec
这时候pydocx就会包含到exe的运行环境里面