from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit, QPushButton, QFileDialog, QVBoxLayout, \ QHBoxLayout, QWidget, QMessageBox from PyQt6.QtCore import QThread, pyqtSignal import sys import pandas as pd import os import datetime class MatchThread(QThread): match_success = pyqtSignal(str) match_failed = pyqtSignal(str) def __init__(self, match_type, sfname, bdh): super().__init__() self.match_type = match_type self.sfname = sfname self.bdh = bdh def run(self): try: df = pd.read_excel('{}'.format(self.sfname)) df['匹配内容'] = df['匹配内容'].astype(str) if self.match_type == "身份证": result = self.bdh[self.bdh['被保险人证件号'].isin(df['匹配内容'].to_list())] elif self.match_type == "电话": result = self.bdh[(self.bdh['付款人电话'].isin(df['匹配内容'].to_list())) | ( self.bdh['团单电话'].isin(df['匹配内容'].to_list())) | ( self.bdh['被保险人电话'].isin(df['匹配内容'].to_list()))] elif self.match_type == "投保人": result = self.bdh[self.bdh['付款人'].isin(df['匹配内容'].to_list())] result.to_excel('{}匹配{}.xlsx'.format(self.match_type, datetime.datetime.now().strftime('%Y%d%m-%H%M%S')), index=False) self.match_success.emit(self.match_type + "匹配成功") except: self.match_failed.emit("检查是否只选择一个文件路径。\n且列名为匹配内容。") class DataLoadThread(QThread): load_finished = pyqtSignal(bool, str,pd.DataFrame) def run(self): try: bdh = pd.read_parquet('{}\\cb.parquet.gzip'.format(os.getcwd())) bdh['被保险人证件号'] = bdh['被保险人证件号'].astype(str) bdh['付款人电话'] = bdh['付款人电话'].astype(str) bdh['团单电话'] = bdh['团单电话'].astype(str) bdh['被保险人电话'] = bdh['被保险人电话'].astype(str) self.bdh = bdh # 传递信息变量出去 self.load_finished.emit(True, '',self.bdh) except Exception as e: self.load_finished.emit(False, str(e),None) class App(QMainWindow): def __init__(self): super().__init__() self.sfname = '' self.bdh = None self.match_thread=None self.setWindowTitle('2022年度参保匹配系统') self.setGeometry(100, 100, 500, 300) # 加入 msg_box self.msg_box = QMessageBox(self) layout = QVBoxLayout() label1 = QLabel('请选择匹配文件:') self.text1 = QLineEdit() button1 = QPushButton('浏览', clicked=self.select_excelfile) label2 = QLabel('请选择匹配方式') label3 = QLabel('匹配前请先加载数据:') label4 = QLabel('再次选择文件请清空路径:', self) label4.setStyleSheet("color: red") button2 = QPushButton('被保险人身份证', clicked=lambda: self.do_process("身份证")) button3 = QPushButton('电话', clicked=lambda: self.do_process("电话")) button4 = QPushButton('投保人', clicked=lambda: self.do_process("投保人")) button5 = QPushButton('退出', clicked=self.close) button5.setStyleSheet("color: red") self.load_thread = DataLoadThread() self.load_thread.load_finished.connect(self.on_load_finished) button6 = QPushButton('加载数据', clicked=self.load_data) button6.setStyleSheet("color: red") button7 = QPushButton('清空', clicked=self.clear_path) button7.setStyleSheet("background-color: red") layout.addWidget(label1) layout.addWidget(self.text1) layout.addWidget(button1) layout.addWidget(label3) layout.addWidget(button6) layout.addWidget(label2) button_layout = QHBoxLayout() button_layout.addWidget(button2) button_layout.addWidget(button3) button_layout.addWidget(button4) button_layout.addWidget(button5) layout.addLayout(button_layout) layout.addWidget(label4) layout.addWidget(button7) central_widget = QWidget() central_widget.setLayout(layout) self.setCentralWidget(central_widget) def on_load_finished(self, success, error=None,bdh=None): # 在主线程中处理加载完成或出错的情况, 接受bdh参数 if success: print("yes") self.bdh=bdh self.msg_box.setText('加载数据成功!') self.msg_box.exec() else: self.msg_box.setText('加载数据失败:{}'.format(error)) self.msg_box.exec() def select_excelfile(self): try: options = QFileDialog.Option(QFileDialog.Option.ReadOnly) # options |= QFileDialog.ReadOnly self.sfname, _ = QFileDialog.getOpenFileName(self, "选择Excel文件", "", "Excel (*.xlsx);;All Files (*)", options=options) self.text1.setText(self.sfname) except Exception as e: print("An error occurred: ", str(e)) def load_data(self): # 启动子线程 self.load_thread.start() def do_process(self, match_type): if self.sfname == '' or self.bdh is None: # if self.sfname == '': QMessageBox.warning(self, '警告', '请选择匹配文件并加载数据!') return else: self.match_thread = MatchThread(match_type, self.sfname, self.bdh) self.match_thread.match_success.connect(self.on_match_success) self.match_thread.match_failed.connect(self.on_match_failed) self.match_thread.start() def on_match_success(self, message): QMessageBox.information(self, '提示', message) def on_match_failed(self, message): QMessageBox.warning(self, '警告', message) def clear_path(self): self.text1.clear() if __name__ == '__main__': app = QApplication(sys.argv) ex = App() ex.show() sys.exit(app.exec())