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进行转换。

相关推荐
Sirens.1 分钟前
MySQL表设计进阶-约束范式连接索引与事务
android·数据库·mysql
字节跳动开源1 小时前
火山引擎开源 Agent 驱动的搜索自迭代技术
数据库·开源·agent
Oo大司命oO3 小时前
藏在正则表达式里的陷阱
数据库·mysql·正则表达式
_oP_i4 小时前
mysql统计数据库使用存储大小
数据库
会编程的土豆5 小时前
MySQL 入门:库、表、行、主键是什么
linux·数据库·网络协议·http
jjjava2.05 小时前
系统日志:从入门到精通的完整指南
网络·数据库
写代码的强哥5 小时前
云原生数据库与传统数据库相比“新”在哪里
数据库·云原生·架构
吴声子夜歌6 小时前
MongoDB 4.2——查询
数据库·mongodb
xieliyu.8 小时前
MySQL 存储过程详解:概念、创建与删除全教程
开发语言·数据库·mysql