python打包exe文件

由于用户需要,将采集数据解析成txt文件,为了方便使用,将python解析方法打包成exe文件供用户使用

安装环境

cpp 复制代码
 ./pip.exe install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple

python导入需要的包

cpp 复制代码
import tkinter as tk
from tkinter import filedialog

打开选择文件

cpp 复制代码
    root = tk.Tk()
    root.withdraw()  # 隐藏主窗口
    file_path = filedialog.askopenfilename(title='请选择一个CHE文件', filetypes=[('Excel','.CHE')])  # 打开文件对话框

写文件过程

cpp 复制代码
#业务代码略过
。。。。。。。
if __name__ == '__main__':
    root = tk.Tk()
    root.withdraw()  # 隐藏主窗口
    file_path = filedialog.askopenfilename(title='请选择一个CHE文件', filetypes=[('Excel','.CHE')])  # 打开文件对话框
    if file_path:
        with open(file_path, 'r') as file:
            full_path = file.name
            che_path = full_path.split("/")[-1]
            de_id = che_path.split('-')[0].split('\\')[-1]
            rec_data = che_path.split('-')[1]
            rec_time = che_path.split('-')[2]

            pa = ParseCHE()
            data_dict = pa.parse(full_path)
            ecg_data = data_dict['ecgList']
            time = data_dict['time']
            ecg_ix = np.arange(0, len(ecg_data))

            ecg_save_name = che_path.split('CHE')[0] + '_ecg.txt'

            with open(ecg_save_name, 'w') as f:
                f.write('Device_ID:' + de_id + '\n')
                f.write('REC_Date:' + rec_data + '\n')
                f.write('REC_Time:' + rec_time + '\n')
                f.write('Sample Rate: 200Hz\n')
                f.write('date' + ' ' + 'time' + ' ' + 'ECG' + ' ' + 'Time(point)\n')
                for i, v in enumerate(ecg_data):
                    f.write(datetime.datetime.fromtimestamp(pa.__time__[int(i / 200)]).strftime(
                        "%Y-%m-%d %H:%M:%S") + ' ' + str(int(v)) + ' ' + str(i) + '\n')
            #########
            rspch_data = data_dict['respList']
            rspab_data = data_dict['respList_ab']
            all_len = len(rspab_data)
            rspch_save_name = che_path.split('CHE')[0] + '_rsp.txt'
            with open(rspch_save_name, 'w') as f:
                f.write('Device_ID:' + de_id + '\n')
                f.write('REC_Date:' + rec_data + '\n')
                f.write('REC_Time:' + rec_time + '\n')
                f.write('Sample Rate: 25Hz\n')
                f.write('date' + ' ' + 'time' + ' ' + 'rspch' + ' ' + 'rspab' + ' ' + 'Time(point)\n')
                for i in range(all_len):
                    f.write(datetime.datetime.fromtimestamp(pa.__time__[int(i / 25)]).strftime(
                        "%Y-%m-%d %H:%M:%S") + ' ' + str(int(rspch_data[i])) + ' ' + str(
                        int(rspab_data[i])) + ' ' + str(i) + '\n')
            #########
            x_data = data_dict['xList']
            y_data = data_dict['yList']
            z_data = data_dict['zList']
            xyz_save_name = che_path.split('CHE')[0] + '_xyz.txt'
            with open(xyz_save_name, 'w') as f:
                f.write('Device_ID:' + de_id + '\n')
                f.write('REC_Date:' + rec_data + '\n')
                f.write('REC_Time:' + rec_time + '\n')
                f.write('Sample Rate: 25Hz\n')
                f.write('date' + ' ' + 'time' + ' ' + 'x' + ' ' + 'y' + ' ' + 'z' + ' ' + 'Time(point)\n')
                for i in range(all_len):
                    f.write(datetime.datetime.fromtimestamp(pa.__time__[int(i / 25)]).strftime(
                        "%Y-%m-%d %H:%M:%S") + ' ' + str(int(x_data[i])) + ' ' + str(int(y_data[i])) + ' ' + str(
                        int(z_data[i])) + ' ' + str(i) + '\n')
            #########
            spo_data = data_dict['spo_value']
            spo_save_name = che_path.split('CHE')[0] + '_spo.txt'
            with open(spo_save_name, 'w') as f:
                f.write('Device_ID:' + de_id + '\n')
                f.write('REC_Date:' + rec_data + '\n')
                f.write('REC_Time:' + rec_time + '\n')
                f.write('Sample Rate: 1Hz\n')
                f.write('date' + ' ' + 'time' + ' ' + 'SPO' + ' ' + 'Time(point)\n')
                for i in range(len(spo_data)):
                    f.write(datetime.datetime.fromtimestamp(pa.__time__[int(i / 25)]).strftime(
                        "%Y-%m-%d %H:%M:%S") + ' ' + str(int(spo_data[i])) + ' ' + str(i) + '\n')

    else:
            print("未选择文件")

打包命令

cpp 复制代码
 ..\venv\Scripts\pyinstaller.exe -F -w  che2txt.py

开始未加-F -w 导致打包出来的exe一闪而过,加上即可解决

相关推荐
曲幽1 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
chlk1235 小时前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑5 小时前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件6 小时前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
敏编程6 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪6 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook6 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
深紫色的三北六号15 小时前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
花酒锄作田19 小时前
使用 pkgutil 实现动态插件系统
python
SudosuBash19 小时前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)