基于PyQt5制作Excel數(shù)據(jù)分組匯總器
在寫數(shù)據(jù)匯總分組工具之前梳理一下需求,要求一:能夠?qū)xcel的數(shù)據(jù)展示到列表中。要求二:能夠支持按列匯總數(shù)據(jù),并且多列分組匯總。要求三:能夠預(yù)覽分組匯總以后的數(shù)據(jù),最后將分好組匯總的數(shù)據(jù)保存到新的excel數(shù)據(jù)文件中。

主要使用到第三方python模塊有下面這些,和前面幾個 PyQt5 應(yīng)用不同的是這次增加了一個樣式模塊 qdarkstyle ,通過最后將這個模塊直接加入到 QApplication 中就可以顯示成黑色酷酷的應(yīng)用了。這個樣式我個人是比較喜歡的...
'''應(yīng)用操作庫''' import sys import os '''應(yīng)用樣式庫''' from qdarkstyle import load_stylesheet_pyqt5 '''UI界面庫''' from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * '''數(shù)據(jù)提取庫''' import pandas as pd
編寫 UI 界面組件布局,UI 布局函數(shù) init_ui()。init_ui() 的函數(shù)整體內(nèi)容都貼在下面這里,大佬們可以根據(jù)自己的需要隨意發(fā)揮。
def init_ui(self):
# 標(biāo)題、圖標(biāo)設(shè)置
self.setWindowTitle('Excel數(shù)據(jù)匯總工具 公眾號:[Python 集中營]')
self.setWindowIcon(QIcon(':/data_sum.ico'))
# 初始化水平布局
hbox = QHBoxLayout()
# 初始化柵格布局
grid = QGridLayout()
self.data_source_text = QLineEdit()
self.data_source_text.setReadOnly(True)
self.data_source_btn = QPushButton()
self.data_source_btn.setText('數(shù)據(jù)')
self.data_source_btn.clicked.connect(self.data_source_btn_click)
self.data_group_column = QLabel()
self.data_group_column.setText('設(shè)置分組列')
self.data_group_column_text = QLineEdit()
self.data_group_column_text.setPlaceholderText('列名1,列名2...')
self.save_dir_text = QLineEdit()
self.save_dir_text.setReadOnly(True)
self.save_dir_btn = QPushButton()
self.save_dir_btn.setText('路徑')
self.save_dir_btn.clicked.connect(self.save_dir_btn_click)
self.view_data_btn = QPushButton()
self.view_data_btn.setText('預(yù)覽數(shù)據(jù)')
self.view_data_btn.clicked.connect(self.view_data_btn_click)
self.save_data_btn = QPushButton()
self.save_data_btn.setText('保存')
self.save_data_btn.clicked.connect(self.save_data_btn_click)
grid.addWidget(self.data_source_text, 0, 0, 1, 2)
grid.addWidget(self.data_source_btn, 0, 2, 1, 1)
grid.addWidget(self.data_group_column, 1, 0, 1, 1)
grid.addWidget(self.data_group_column_text, 1, 1, 1, 2)
grid.addWidget(self.save_dir_text, 2, 0, 1, 2)
grid.addWidget(self.save_dir_btn, 2, 2, 1, 1)
grid.addWidget(self.view_data_btn, 3, 0, 1, 2)
grid.addWidget(self.save_data_btn, 3, 2, 1, 1)
self.table_view = QTableView()
self.table_view.setFixedWidth(500)
self.table_view.setFixedHeight(100)
hbox.addWidget(self.table_view)
hbox.addLayout(grid)
self.setLayout(hbox)
槽函數(shù)總共使用了四個,分別是下面這些。
save_data_btn_click:將分組匯總后的 DataFrame 數(shù)據(jù)直接保存。
data_source_btn_click:用來加載需要分組匯總的 excel 文件的,并將加載出來的 DataFrame 數(shù)據(jù)直接顯示到 QTableView 的組件上面,這樣可以實時的看見加載進(jìn)來的原始數(shù)據(jù)。
save_dir_btn_click:點(diǎn)擊選擇存儲路徑時觸發(fā)的槽函數(shù),用來調(diào)起 QFileDialog 來選擇文件路徑。
view_data_btn_click:調(diào)起預(yù)覽分組匯總后的數(shù)據(jù),將分組后的數(shù)據(jù)顯示到窗口上。
槽函數(shù) data_source_btn_click,加載 excel 源數(shù)據(jù)。
def data_source_btn_click(self):
xlsx_file = QFileDialog.getOpenFileName(self, '選擇文件', self.cwd, 'Excel File(*.xlsx)')
self.data_source_text.setText(xlsx_file[0])
self.data_frame = pd.read_excel(self.data_source_text.text().strip())
print(self.data_frame)
model = TableModelView(self.data_frame)
self.table_view.setModel(model)
槽函數(shù) save_data_btn_click,保存最終的 excel 數(shù)據(jù)。
def save_data_btn_click(self):
dir = self.save_dir_text.text().strip()
self.data_frame_group.to_excel(dir + 'group_data.xlsx',sheet_name='數(shù)據(jù)信息匯總')
槽函數(shù) view_data_btn_click,預(yù)覽分組匯總的數(shù)據(jù)。
def view_data_btn_click(self):
columns = self.data_group_column_text.text().strip()
column_list = []
if columns != '':
column_list = columns.split(',')
self.data_frame_group = self.data_frame.groupby(column_list, as_index=False).sum()
print(self.data_frame_group)
model = TableModelView(self.data_frame_group)
self.table_view.setModel(model)
槽函數(shù) save_dir_btn_click,存儲文件選擇。
def save_dir_btn_click(self):
save_path = QFileDialog.getExistingDirectory(self, '選擇文件夾', self.cwd)
self.save_dir_text.setText(save_path + '/')
最后,在主要代碼塊中展示列表數(shù)據(jù)時時使用了一個 QTableView 組件自定義的模型 TableModelView。
到此這篇關(guān)于基于PyQt5制作Excel數(shù)據(jù)分組匯總器的文章就介紹到這了,更多相關(guān)PyQt5 Excel數(shù)據(jù)分組匯總器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python正則表達(dá)式re.sub各個參數(shù)的超詳細(xì)講解
Python 的 re 模塊提供了re.sub用于替換字符串中的匹配項,下面這篇文章主要給大家介紹了關(guān)于python正則表達(dá)式re.sub各個參數(shù)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
比較詳細(xì)Python正則表達(dá)式操作指南(re使用)
Python 1.5之前版本則是通過 regex 模塊提供 Emecs 風(fēng)格的模式。Emacs 風(fēng)格模式可讀性稍差些,而且功能也不強(qiáng),因此編寫新代碼時盡量不要再使用 regex 模塊,當(dāng)然偶爾你還是可能在老代碼里發(fā)現(xiàn)其蹤影2008-09-09
python matplotlib中的subplot函數(shù)使用詳解
今天小編就為大家分享一篇python matplotlib中的subplot函數(shù)使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

