需要剖析:

当初有一大堆的Excel数据文件,须要依据每个Excel数据文件外面的Sheet批量将数据文件合并成为一个汇总后的Excel数据文件。或者是将一个汇总后的Excel数据文件依照Sheet拆分成很多个Excel数据文件。依据下面的需要,咱们先来进行UI界面的布局设计。

【浏览全文】

残缺源代码请滑到文章最初获取,感激大家的反对!

导入UI界面设计相干的PyQt5模块

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

利用操作相干的模块

import sysimport os

excel 数据处理模块

import openpyxl as pxlimport pandas as pd

看一下 UI 界面的性能和布局,感觉还能够...

上面是布局相干的代码块实例

    def init_ui(self):        self.setWindowTitle('Excel数据汇总/拆分器  公众号:[Python 集中营]')        self.setWindowIcon(QIcon('数据.ico'))        self.brower = QTextBrowser()        self.brower.setReadOnly(True)        self.brower.setFont(QFont('宋体', 8))        self.brower.setPlaceholderText('批量数据处理进度显示区域...')        self.brower.ensureCursorVisible()        self.excels = QLineEdit()        self.excels.setReadOnly(True)        self.excels_btn = QPushButton()        self.excels_btn.setText('加载批文件')        self.excels_btn.clicked.connect(self.excels_btn_click)        self.oprate_type = QLabel()        self.oprate_type.setText('操作类型')        self.oprate_combox = QComboBox()        self.oprate_combox.addItems(['数据合并', '数据拆分'])        self.data_type = QLabel()        self.data_type.setText('合并/拆分')        self.data_combox = QComboBox()        self.data_combox.addItems(['依照Sheet拆分'])        self.new_file_path = QLineEdit()        self.new_file_path.setReadOnly(True)        self.new_file_path_btn = QPushButton()        self.new_file_path_btn.setText('新文件门路')        self.new_file_path_btn.clicked.connect(self.new_file_path_btn_click)        self.thread_ = DataThread(self)        self.thread_.trigger.connect(self.update_log)        self.thread_.finished.connect(self.finished)        self.start_btn = QPushButton()        self.start_btn.setText('开始数据汇总/拆分')        self.start_btn.clicked.connect(self.start_btn_click)        form = QFormLayout()        form.addRow(self.excels, self.excels_btn)        form.addRow(self.oprate_type, self.oprate_combox)        form.addRow(self.data_type, self.data_combox)        form.addRow(self.new_file_path, self.new_file_path_btn)        vbox = QVBoxLayout()        vbox.addLayout(form)        vbox.addWidget(self.start_btn)        hbox = QHBoxLayout()        hbox.addWidget(self.brower)        hbox.addLayout(vbox)        self.setLayout(hbox)

槽函数 update_log,将运行过程通过文本浏览器的形式实时展现,不便查看程序的运行。

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

槽函数 excels_btn_click,绑定到文件加载按钮,解决源文件的加载过程。

 def excels_btn_click(self):        paths = QFileDialog.getOpenFileNames(self, '抉择文件', os.getcwd(), 'Excel File(*.xlsx)')        files = paths[0]        path_strs = ''        for file in files:            path_strs = path_strs + file + ';'        self.excels.setText(path_strs)        self.update_log('曾经实现批文件门路加载!')

槽函数 new_file_path_btn_click,抉择新文件要保留的门路。

 def new_file_path_btn_click(self):        directory = QFileDialog.getExistingDirectory(self, '抉择文件夹', os.getcwd())        self.new_file_path.setText(directory)

槽函数 start_btn_click,绑定到开始按钮上,应用开始按钮启动子线程工作。

    def start_btn_click(self):        self.start_btn.setEnabled(False)        self.thread_.start()

函数 finished,这个函数是用来接管子线程传过来的运行实现的信号,通过判断使子线程执行实现时让开始按钮处于能够点击的状态。

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

上面是最重要的逻辑解决局部,将所有的逻辑解决相干的局部全副放到子线程中去执行。

class DataThread(QThread):    trigger = pyqtSignal(str)    finished = pyqtSignal(bool)    def __init__(self, parent=None):        super(DataThread, self).__init__(parent)        self.parent = parent        self.working = True    def __del__(self):        self.working = False        self.wait()    def run(self):        self.trigger.emit('启动批量解决子线程...')        oprate_type = self.parent.oprate_combox.currentText().strip()        data_type = self.parent.data_combox.currentText().strip()        files = self.parent.excels.text().strip()        new_file_path = self.parent.new_file_path.text()        if data_type == '依照Sheet拆分' and oprate_type == '数据合并':            self.merge_data(files=files, new_file_path=new_file_path)        elif data_type == '依照Sheet拆分' and oprate_type == '数据拆分':            self.split_data(files=files, new_file_path=new_file_path)        else:            pass        self.trigger.emit('数据处理实现...')        self.finished.emit(True)    def merge_data(self, files, new_file_path):        num = 1        new_file = new_file_path + '/数据汇总.xlsx'        writer = pd.ExcelWriter(new_file)        for file in files.split(';'):            if file.strip() != '':                web_sheet = pxl.load_workbook(file)                sheets = web_sheet.sheetnames                for sheet in sheets:                    sheet_name = sheet.title()                    self.trigger.emit('筹备解决工作表名称:' + str(sheet.title()))                    data_frame = pd.read_excel(file, sheet_name=sheet_name)                    sheet_name = sheet_name + 'TO数据合并' + str(num)                    data_frame.to_excel(writer, sheet_name, index=False)                    num = num + 1            else:                self.trigger.emit('以后门路为空,持续...')        writer.save()        writer.close()    def split_data(self, files, new_file_path):        num = 1        for file in files.split(';'):            if file.strip() != '':                web_sheet = pxl.load_workbook(file)                sheets = web_sheet.sheetnames                for sheet in sheets:                    sheet_name = sheet.title()                    self.trigger.emit('筹备解决工作表名称:' + str(sheet.title()))                    data_frame = pd.read_excel(file, sheet_name=sheet_name)                    writer = pd.ExcelWriter(new_file_path + '/数据拆分' + str(num) + '.xlsx')                    data_frame.to_excel(writer, '数据拆分', index=False)                    writer.save()                    writer.close()                    num = num + 1            else:                self.trigger.emit('以后门路为空,持续...')

下面就是次要的代码块实现过程,有须要的能够参考一下。欢送大佬在评论区进行留言。

搞了一个程序运行效果图,看一下执行成果。

公众号内回复 "Excel批量数据文件拆分与合并",获取残缺源代码,间接运行即可。

【往期精选】

办公自动化:PDF文件合并器,将多个PDF文件进行合并...

GUI猜数字游戏,间接开玩...

手把手教你做一个数据图表生成器(附源码)...

动静指针时钟:利用pyqt5制作指针钟表显示实时工夫

python 日志中最亮的仔,是喜爱的花里胡哨吖...