如何将包含多个数据文件的Python文件转换为.exe格式



我是Python和编程的新手。我试着写代码,需要一个.docx,一个.xlsx和一个图像文件才能正常工作。当用py2exe将其转换为.exe格式时,出现了问题,我的程序无法打开!我刚刚更改了setup.py文件如下:

import os
from distutils.core import setup
import py2exe
import tkinter as tk
from tkinter import ttk
import sys
import os
import pandas as pd
from docx import Document
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
Mydata_files = [('Data.docx', ['C:/Users/Murat/Desktop/Combobox/Data.docx']),
('Data.xlsx', ['C:/Users/Murat/Desktop/Combobox/Data.xlsx']),
('Logo.gif', ['C:/Users/Murat/Desktop/Combobox/Logo.gif'])]
setup(console=['Combobox.py'],
data_files = Mydata_files,
options={"py2exe":{"includes": ["lxml._elementpath"], "unbuffered": True, "optimize": 2}})

现在,我可以在dist文件夹中看到我的数据文件,但我的代码引发了权限错误:

Traceback (most recent call last):
File "Combobox.py", line 94, in <module>
File "pandasutil_decorators.pyc", line 299, in wrapper
File "pandasioexcel_base.pyc", line 336, in read_excel
File "pandasioexcel_base.pyc", line 1057, in _init_
File "pandasioexcel_base.pyc", line 938, in inspect_excel_format
File "pandasiocommon.pyc", line 651, in get_handle
PermissionError: [Errno 13] Permission denied: 'Data.xlsx

我的代码中关于Data.xlsx文件的行如下:

df=pd.read_excel("Data.xlsx")

在关闭控制台之前,您应该在最后使用input((命令等待用户

此外,在编写脚本时请始终使用main,以免在导入时出现奇怪的行为

if __name__ == '__main__': #Use dunder method as a best practice
Mydata_files = []
for files in os.listdir("C:/Users/Murat/Desktop/Combobox/"):
f1 = "C:/Users/Murat/Desktop/Combobox/" + files
if os.path.isfile(f1):
f2 = 'images', [f1]
Mydata_files.append(f2)
setup(
console=['trypyglet.py.py'],
data_files=Mydata_files,
options={
"py2exe": {
"unbuffered": True,
"optimize": 2
}
}
)
input = input() # Use input to wait for the user

相关内容

  • 没有找到相关文章

最新更新