python - Pyinstaller - 将 excel 文件捆绑到 onefile exe
问题描述
我使用包含 excel 文件的 pyinstaller 创建了一个 EXE 文件。看下面的代码:
import pandas
from tkinter import *
sec = pandas.read_csv("sectionsize.csv")
win = Tk()
win.title('Imp vs Metric Section size')
win.geometry('320x100')
win.attributes("-topmost", True)
win.resizable(0,0)
def reset(dummy=None):
e1.delete(0, 'end')
e2.delete(0, 'end')
def conv(dummy=None):
s1 = str(e1.get())
s2 = str(e2.get())
a = s1.upper()
b = s2.upper()
if len(a)!=0 and len(b)==0:
try:
imp_index = sec[sec['Imperial']==a].index[0]
x = sec['Metric'][imp_index]
e2.delete(0, 'end')
e2.insert(END, x)
except:
x = 'Check Section size'
e2.delete(0, 'end')
e2.insert(END, x)
elif len(a)==0 and len(b)!=0:
try:
metric_index = sec[sec['Metric']==b].index[0]
y = sec['Imperial'][metric_index]
e1.delete(0, 'end')
e1.insert(END, y)
except:
y = 'Check Section size'
e1.delete(0, 'end')
e1.insert(END, y)
elif len(a)!=0 and len(b)!=0:
e1.delete(0, 'end')
e2.delete(0, 'end')
else:
pass
win.bind('<Return>', conv)
win.bind('<Escape>', reset)
l1=Label(win, text='Imperial')
l1.place(x=25, y=10)
l2=Label(win, text='Metric')
l2.place(x=25, y=50)
e1=Entry(win, width=30)
e1.place(x=90, y=10)
e1.focus_set()
e2=Entry(win, width=30)
e2.place(x=90, y=50)
win.mainloop()
exe 文件在我的计算机上打开并完美运行。唯一的问题是此文件要求 excel 文件位于同一文件夹中。如何在不需要外部文件的情况下将 excel 文件捆绑到 EXE 中?任何输入都会非常有帮助。
解决方案
为此,您必须使用--add-data
标志
来自 Pyinstaller 的文档:
捆绑什么,在哪里搜索:--add-data <SRC;DEST or SRC:DEST> 要添加到可执行文件的其他非二进制文件或文件夹。路径分隔符是特定于平台的,
os.pathsep
(;
在 >Windows 和:
大多数 unix 系统上)被使用。此 > 选项可以多次使用。
推荐阅读
- c++ - 为什么函数不需要时传递`this`?
- node.js - Typeorm 不会等待不保存
- python - Numba 打字错误:numba.cores.errors.TypingError:np.random.binomial
(数组(float64, 2d, C), float64) - c# - 如何控制进程的执行时间并在c#中跨类处理异常
- javascript - 将嵌套数组与另一个数组进行比较,如果值相同则赋值
- python - Django 频道,属性错误
- c# - 如何将 insert into 用于多个表?
- javascript - 无法使用本地 chrome 打开 VS 代码扩展预览服务器
- c# - 当模拟用户没有域访问权限时,远程将域用户添加为管理员
- javascript - 无法以角度提交我的默认 HTML 表单