热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【Python技巧】十分钟就能够实现Excel数据整合/拆分,为啥要花一天?|附源码

相关文件想学Python的小伙伴可以关注小编的公众号【Python日志】有很多的资源可以白嫖的哈,不定时会更新一下Python的小知识的哈!ÿ

相关文件

想学Python的小伙伴可以关注小编的公众号【Python日志】
有很多的资源可以白嫖的哈,不定时会更新一下Python的小知识的哈!!
需要源码的小伙伴可以在公众号回复Excel表格
Python源码、问题解答学习交流群:773162165


需求分析

现在有一大堆的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数据汇总/拆分器')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 = parentself.working = Truedef __del__(self):self.working = Falseself.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:passself.trigger.emit('数据处理完成...')self.finished.emit(True)def merge_data(self, files, new_file_path):num = 1new_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.sheetnamesfor 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 + 1else:self.trigger.emit('当前路径为空,继续...')writer.save()writer.close()def split_data(self, files, new_file_path):num = 1for file in files.split(';'):if file.strip() != '':web_sheet = pxl.load_workbook(file)sheets = web_sheet.sheetnamesfor 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 + 1else:self.trigger.emit('当前路径为空,继续...')

上面就是主要的代码块实现过程,有需要的可以参考一下。欢迎大佬在评论区进行留言。


效果展示

搞了一个程序运行效果图,看一下执行效果。在这里插入图片描述

公众号:Python日志
公众号内回复 Excel表格,获取完整源代码,直接运行即可。


推荐阅读
author-avatar
yun悠然_434
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有