|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 yesterday0931 于 2019-3-9 07:16 编辑
- import os
- import re
- pyname = os.listdir('C:/Users/XXX/Desktop/python_compile/input')[0]
- input_path = 'C:/Users/XXX/Desktop/python_compile/input/'
- output_path = 'C:/Users/XXX/Desktop/python_compile/output/'
- py_path = input_path + pyname
- filename = pyname.split('.')[0]
- specname = filename + '.spec'
- spec_path = output_path + specname
- os.system('pyi-makespec ' + py_path + ' --specpath ' + output_path)
- image_list = []
- image_name = []
- with open(py_path,'r', encoding='utf-8') as py_f:
- for line in py_f:
- temp = re.findall(r"\w:.*\.[pj][np]g", line)
- if temp:
- image_list.append(temp[0])
- name = re.search(r"((?!\\).)*\.(png|jpg)", temp[0]).group()
- image_name.append(name)
- with open(spec_path) as spec_f:
- insert_tuple = ""
- insert_temp = ""
- insert_string = ""
- l = len(image_name)
- lines = spec_f.readlines()
- for n in range(l):
- if l == 1:
- insert_temp = "('%s','%s','DATA')" % (image_name[n], image_list[n])
- elif l > 1:
- insert_temp = "('%s','%s','DATA')" % (image_name[n], image_list[n])
- if n == 0:
- insert_tuple = insert_temp
- else:
- insert_tuple = insert_tuple + ',' + insert_temp
- insert_string = "a.datas += (" + insert_tuple + ")\n"
- lines[17:17] = insert_string
- open(spec_path, 'w').writelines(lines)
- build_path = output_path + filename + '/build'
- dist_path = output_path + filename + '/dist'
- os.system('pyinstaller '+ ' -F ' + ' --distpath ' + dist_path + ' --workpath ' + build_path + ' ' + spec_path)
复制代码
以上为代码。
作用:将带图片资源的py 一键打包为 exe
前提: 安装好pyinstaller
方法:将XXX改为你的Windows用户名(也可自行修改),在桌面新建python_compile/input/ 文件夹,python_compile/output/文件夹 ,在input里面放入你要编译的py文档,
运行脚本会在output里得到输出,主要是dist文件夹。
注意:py文件中图片资源的路径必须为绝对路径,如:C:\\Users\\XXX\\Desktop\\python_compile\\input\\res\\gray_ball.png
小Tips:可将该py文件放入input文件夹,打包生成compile.exe,实现一键打包。 |
|