分享

Excel数据处理器整合版(附源码)!

 Python集中营 2022-10-10 发布于甘肃

开始介绍之前看一下实现以后的效果图长什么样,之所以将效果图放在前面方便有需要的朋友可以学习,不耽误已经学会的朋友时间。

1. 准备

在Excel数据处理器实现过程中,需要使用到的通用的python模块这里列举了出来,可以参考对自己本地没有的python模块进行安装。

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

import sys
import os

import traceback

将通用模块的python模块准备好就可以开始下面的开发阶段了。由于篇幅相对较长,若是直接需要exe可运行程序的朋友可以直接滑到文章底部获取下载方式进行下载。

2. 主页面

主界面的实现我们这里是通过继承QTabWidget的容器来实现的,因为我们只需要在页面中添加一些各种各样的标签页,不需要添加菜单栏、工具栏、状态栏,所以并没有使用主窗口的QMainWindow的容器来实现。

class MainWindow(QTabWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle('Excel数据处理工具(整合版) +关注公众号:Python 集中营,一起学习更多干货!')
        self.setWindowIcon(QIcon('excel.ico'))
        self.setFixedSize(700400)

        self.xls_or_db = DataDB()
        self.data_no_repeat = DataNoRept()
        self.data_spt_mer = DataSptMer()
        self.data_info_sum = DataInfoSum()
        self.text_format_conversion = TextFormatConversion()

        self.addTab(self.xls_or_db, '数据库W/R')
        self.addTab(self.data_no_repeat, 'Excel数据去重')
        self.addTab(self.data_spt_mer, 'Excel拆分/合并')
        self.addTab(self.data_info_sum, 'Excel信息汇总')
        self.addTab(self.text_format_conversion, '文本格式转换')

可以看到在主页面中通过添加了五个容器作为标签页,只需要点击相应的标签页就可以看到不同的容器中对应的ui页面,接下来就只需要去实现每个标签页对应的容器中的布局就OK了。

在接下来的实现过程中,为了保证业务处理和UI主页面的执行更加高效,我们是通过在主线程中展示页面及主体循环逻辑,再通过单独的子线程来实现具体的业务逻辑。这样就不会出现主线程中的页面经常会因为业务执行而卡死的情况。

3. 数据库读写

功能介绍

数据库读写实现的就是Mysql数据库与excel文件之间的读写功能,可以将excel文件批量导入数据库中,并且能将数据库的数据通过sql读取并保存为excel文件。

UI界面代码

下面是数据库与Excel读写实现的UI界面布局,大家有兴趣也可以调整为自己喜欢的布局。布局页面实现是通过继承QWidget容器中的组件来实现的。

class DataDB(QWidget):
    def __init__(self):
        super(DataDB, self).__init__()
        self.init_ui()

    def init_ui(self):
        vbox1 = QVBoxLayout()
        self.db_brower = QTextBrowser()
        self.db_brower.setFont(QFont('宋体'8))
        self.db_brower.setReadOnly(True)
        self.db_brower.setPlaceholderText('处理进程展示区域...')
        self.db_brower.ensureCursorVisible()
        vbox1.addWidget(self.db_brower)

        vbox2 = QVBoxLayout()
        vbox2_form1 = QFormLayout()

        self.db_ip_lab = QLabel()
        self.db_ip_lab.setText('数据库地址 ')

        self.db_ip_in = QLineEdit()
        self.db_ip_in.setPlaceholderText('0.0.0.0')

        self.db_port_lab = QLabel()
        self.db_port_lab.setText('数据库端口 ')

        self.db_port_in = QLineEdit()
        self.db_port_in.setPlaceholderText('1521')

        self.db_user_lab = QLabel()
        self.db_user_lab.setText('数据库用户名 ')

        self.db_user_in = QLineEdit()
        self.db_user_in.setPlaceholderText('admin')

        self.db_pwd_lab = QLabel()
        self.db_pwd_lab.setText('数据库密码 ')

        self.db_pwd_in = QLineEdit()
        self.db_pwd_in.setPlaceholderText('admin123')

        self.db_name_lab = QLabel()
        self.db_name_lab.setText('数据库名称 ')

        self.db_name_in = QLineEdit()
        self.db_name_in.setPlaceholderText('test')

        self.db_table_lab = QLabel()
        self.db_table_lab.setText('数据库表名 ')

        self.db_table_in = QLineEdit()
        self.db_table_in.setPlaceholderText('datadb')

        self.db_oprtype_lab = QLabel()
        self.db_oprtype_lab.setText('操作类型 ')

        self.db_oprtype_combox = QComboBox()
        self.db_oprtype_combox.addItems(['导入数据库''导出Excel'])

        self.import_type_lab = QLabel()
        self.import_type_lab.setText('导入类型 ')

        self.import_type_combox = QComboBox()
        self.import_type_combox.addItems(['追加导入数据''替换相同数据'])

        self.db_file_in = QLineEdit()
        self.db_file_in.setPlaceholderText('文件夹路径')
        self.db_file_in.setReadOnly(True)

        self.db_file_btn = QPushButton()
        self.db_file_btn.setText('路径(.xlsx)')
        self.db_file_btn.clicked.connect(self.db_file_btn_click)

        self.db_start_btn = QPushButton()
        self.db_start_btn.setText('开始运行导入/导出')
        self.db_start_btn.clicked.connect(self.db_start_btn_click)

        vbox2_form1.addRow(self.db_ip_lab, self.db_ip_in)
        vbox2_form1.addRow(self.db_port_lab, self.db_port_in)
        vbox2_form1.addRow(self.db_user_lab, self.db_user_in)
        vbox2_form1.addRow(self.db_pwd_lab, self.db_pwd_in)
        vbox2_form1.addRow(self.db_name_lab, self.db_name_in)
        vbox2_form1.addRow(self.db_table_lab, self.db_table_in)
        vbox2_form1.addRow(self.db_oprtype_lab, self.db_oprtype_combox)
        vbox2_form1.addRow(self.import_type_lab, self.import_type_combox)
        vbox2_form1.addRow(self.db_file_in, self.db_file_btn)

        vbox2.addLayout(vbox2_form1)
        vbox2.addWidget(self.db_start_btn)

        hbox = QHBoxLayout()
        hbox.addLayout(vbox1)
        hbox.addLayout(vbox2)
        self.setLayout(hbox)

        self.thread_ = DataDBThread(self)
        self.thread_.message.connect(self.show_db_message)
        self.thread_.finished.connect(self.finished)

    def show_db_message(self, text):
        cursor = self.db_brower.textCursor()
        cursor.movePosition(QTextCursor.End)
        self.db_brower.append(text)
        self.db_brower.setTextCursor(cursor)
        self.db_brower.ensureCursorVisible()

    def db_file_btn_click(self):
        dir = QFileDialog.getExistingDirectory(self, "选择文件夹", os.getcwd())
        self.db_file_in.setText(dir)

    def db_start_btn_click(self):
        self.db_start_btn.setEnabled(False)
        self.thread_.start()

    def finished(self, finished):
        if finished is True:
            self.db_start_btn.setEnabled(True)

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多