我在 python 中编写了代码,它打开了一个模板 excel 文件。每天午夜,它都会将该模板复制到一个新的 Excel 文件中,代码将在其中记录当天的数据。我的目标是使用 pyinstaller 创建一个包含我的代码和模板 excel 文件的单个可执行文件。
基本上我希望能够通过将excel文件捆绑到从pyinstaller获得的exe文件中来打开模板excel文件,无论计算机是否包含该文件:
现在我打开excel文件,如下所示:
import os
import openpyxl
theFile = openpyxl.load_workbook(os.getcwd()+"\templateExcel.xlsx")
currentSheet = theFile[theFile.sheetnames[0]]
但是,当我将 excel 文件作为
--add-data "templateExcel.xlsx;templateExcel.xlsx
包含在 pyinstaller 命令中时,并运行exe文件,无法检测到templateExcel文件的位置。我知道通过在另一台计算机上运行, os.getcwd() 给出了不同的路径,因此它显然无法打开 excel 文件。因此,我需要一种将 excel 文件捆绑到 exe 文件中的方法,以便无论计算机如何,python 代码都可以找到它。
最佳答案
您可以使用;
Adding a data file in Pyinstaller using the onefile option
总结:
pyinstaller --onefile --nowindow --add-data text.txt;included winprint.py --distpath .
和示例 python 脚本:
import os
import sys
os.chdir(sys._MEIPASS)
os.system('included\\text.txt')
关于python - 在 Pyinstaller 中包含带有程序的 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61236796/