首页 > 解决方案 > 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 中?任何输入都会非常有帮助。

标签: pythonpandastkinterpyinstaller

解决方案


为此,您必须使用--add-data标志

来自 Pyinstaller 的文档:

捆绑什么,在哪里搜索:​​--add-data <SRC;DEST or SRC:DEST> 要添加到可执行文件的其他非二进制文件或文件夹。路径分隔符是特定于平台的,os.pathsep;在 >Windows 和:大多数 unix 系统上)被使用。此 > 选项可以多次使用。


推荐阅读