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一闪而过,加上即可解决

相关推荐
兵慌码乱7 小时前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
金銀銅鐵9 小时前
[Python] 体验用欧几里得算法计算最大公约数的过程
python·数学
FreakStudio13 小时前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
AlfredZhao13 小时前
Docker 容器时区不对,`timedatectl` 不存在怎么办?
linux·timezone
用户03321266636714 小时前
使用 Python 从零创建 Word 文档
python
Csvn18 小时前
Python 两大经典坑点 —— 可变默认参数 & 闭包延迟绑定
后端·python
曲幽20 小时前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
用户5569188175321 小时前
#从脚本到独立程序:Python + Playwright 批量抓取的完整踩坑记录
python·自动化运维
兵慌码乱1 天前
基于 MediaPipe 与 PySide2 的手势交互音乐控制系统实现:轻量化视觉交互全流程解析
python·opencv·计算机视觉·人机交互·手势识别·mediapipe·pyside2
luckdewei2 天前
FastAPI 资产管理系统实战:复杂 ORM 关联、Alembic 迁移与 N+1 查询优化
python