Python & PyQt5 实现 .his 文件批量转 Excel 工具

import os

from PyQt5.QtWidgets import QApplication, QFileDialog, QMessageBox

from his2excel import His2Excel

def convert_his_to_excel(his_files):

"""

将 .his 文件批量转换为 Excel 文件

:param his_files: 要转换的 .his 文件列表

"""

for his_file in his_files:

excel_file = os.path.splitext(his_file)0 + '.xlsx'

if os.path.exists(excel_file):

QMessageBox.warning(None, "警告", f"文件已存在,跳过:{excel_file}")

continue

try:

His2Excel.convert(his_file, excel_file)

print(f"转换成功:{excel_file}")

except Exception as e:

QMessageBox.critical(None, "错误", f"转换失败:{e}")

def select_his_files():

"""

弹出文件选择对话框,选择 .his 文件

:return: 选择的 .his 文件列表

"""

his_files, _ = QFileDialog.getOpenFileNames(None, "选择 .his 文件", "", "HIS Files (*.his)")

return his_files

def main():

app = QApplication(\[\])

his_files = select_his_files()

if his_files:

convert_his_to_excel(his_files)

else:

QMessageBox.information(None, "信息", "没有选择文件,程序将退出。")

if name == 'main':

main()

这段代码首先定义了一个convert_his_to_excel函数,用于将单个或多个.his文件转换为Excel文件。它使用了os.path.splitext来获取文件的基础名称,并添加.xlsx作为Excel文件的扩展名。如果目标Excel文件已存在,会弹出警告消息。如果转换过程中发生异常,会弹出错误消息。select_his_files函数用于弹出文件选择对话框,让用户选择.his文件。main函数则是程序的入口点,它启动Qt应用程序,调用select_his_files获取文件列表,并调用convert_his_to_excel进行转换。

相关推荐
ClickHouseDB4 小时前
ClickHouse Cloud 与 Snowflake 实时性价比对比:CostBench 测试中的 412 倍差距来
运维·数据库·系统架构
m0_639906944 小时前
Redis入门|五种数据结构、使用场景、持久化RDB&AOF
数据结构·数据库·redis
时间售卖铺5 小时前
触发器与函数实战:MySQL高效编程秘诀
运维·数据库·mysql·云原生
qetfw5 小时前
UOS MariaDB 数据库:安装、最小权限与备份验证
linux·数据库·mariadb·uos
数智园区合作社6 小时前
问数平台怎么把同一个指标锁成一个数
大数据·数据库·人工智能·ai
lbb 小魔仙7 小时前
Windows搭建PaddleOCR+飞书Webhook自动打卡通知机器人:识别、推送与远程触发完整教程
数据库·人工智能·prometheus·时序数据库
captain3768 小时前
MyBatis操作数据库(入门2)
java·数据库·spring boot·后端·spring·mybatis
心中有你02148 小时前
Java实现Dijkstra最短路径算法(邻接表 + 优先队列)
java·数据库·算法
龙亘川8 小时前
综合素质评价业务建模:从过程性评价到成长档案全链路闭环
数据库·智慧城市·数据可视化
数据库小组8 小时前
NineData 实战教程:MySQL 5.5 数据迁移同步到 MySQL 8.0(跨版本升级)
数据库·mysql·mysql优化·数据库开发·数据库迁移·版本升级·数据复制