python - 在 Pyinstaller 中包含带有程序的 excel 文件

标签 python excel pyinstaller

我在 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/

相关文章:

vba - 循环遍历类属性?

python - bundle (只是--onefile)

python - 有没有办法在使用 pyinstaller 创建 exe 后录制视频

python - 赋值中的类型不兼容(表达式的类型为 "List[<nothing>]",变量的类型为 (...)

python - 将从 keras.backend.argmax 返回的张量作为索引传递给 keras.backend,gather 预计为 'An integer tensor of indices.'

java - 如何使用java在空单元格上写入?

EXcel VBA 对象被破坏

Python 和 Pyinstaller : include binary application in code

python pandas groupby排序和连接

python - 为什么无法将 Application Insights 与 Azure Function 结合使用?