python打包exe文件

发布于:2024-12-22 ⋅ 阅读:(13) ⋅ 点赞:(0)

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

安装环境

 ./pip.exe install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple

python导入需要的包

import tkinter as tk
from tkinter import filedialog

打开选择文件

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

写文件过程

#业务代码略过
。。。。。。。
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("未选择文件")


打包命令

 ..\venv\Scripts\pyinstaller.exe -F -w  che2txt.py

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