Pyinstaller-将excel文件捆绑到onefile.exe中



我使用pyinstaller创建了一个EXE文件,其中包括excel文件。请参阅以下代码:

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的文档:

捆绑内容,搜索位置:--添加数据<SRC;DEST或SRC:DEST>要添加的其他非二进制文件或文件夹>到可执行文件。路径分隔符为平台特定,os.pathsep(在>Windows上为;以及大多数unix系统上的:(。这>选项可以多次使用。

最新更新