基于PyQt5制作Excel數(shù)據(jù)分組匯總器
在寫(xiě)數(shù)據(jù)匯總分組工具之前梳理一下需求,要求一:能夠?qū)xcel的數(shù)據(jù)展示到列表中。要求二:能夠支持按列匯總數(shù)據(jù),并且多列分組匯總。要求三:能夠預(yù)覽分組匯總以后的數(shù)據(jù),最后將分好組匯總的數(shù)據(jù)保存到新的excel數(shù)據(jù)文件中。
主要使用到第三方python模塊有下面這些,和前面幾個(gè) PyQt5 應(yīng)用不同的是這次增加了一個(gè)樣式模塊 qdarkstyle ,通過(guò)最后將這個(gè)模塊直接加入到 QApplication 中就可以顯示成黑色酷酷的應(yīng)用了。這個(gè)樣式我個(gè)人是比較喜歡的...
'''應(yīng)用操作庫(kù)''' import sys import os '''應(yīng)用樣式庫(kù)''' from qdarkstyle import load_stylesheet_pyqt5 '''UI界面庫(kù)''' from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * '''數(shù)據(jù)提取庫(kù)''' import pandas as pd
編寫(xiě) 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ù)匯總工具 公眾號(hào):[Python 集中營(yíng)]') 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ù)總共使用了四個(gè),分別是下面這些。
save_data_btn_click:將分組匯總后的 DataFrame 數(shù)據(jù)直接保存。
data_source_btn_click:用來(lái)加載需要分組匯總的 excel 文件的,并將加載出來(lái)的 DataFrame 數(shù)據(jù)直接顯示到 QTableView 的組件上面,這樣可以實(shí)時(shí)的看見(jiàn)加載進(jìn)來(lái)的原始數(shù)據(jù)。
save_dir_btn_click:點(diǎn)擊選擇存儲(chǔ)路徑時(shí)觸發(fā)的槽函數(shù),用來(lái)調(diào)起 QFileDialog 來(lái)選擇文件路徑。
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,存儲(chǔ)文件選擇。
def save_dir_btn_click(self): save_path = QFileDialog.getExistingDirectory(self, '選擇文件夾', self.cwd) self.save_dir_text.setText(save_path + '/')
最后,在主要代碼塊中展示列表數(shù)據(jù)時(shí)時(shí)使用了一個(gè) QTableView 組件自定義的模型 TableModelView。
到此這篇關(guān)于基于PyQt5制作Excel數(shù)據(jù)分組匯總器的文章就介紹到這了,更多相關(guān)PyQt5 Excel數(shù)據(jù)分組匯總器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django model 中設(shè)置聯(lián)合約束和聯(lián)合索引的方法
今天小編就為大家分享一篇Django model 中設(shè)置聯(lián)合約束和聯(lián)合索引的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Linux下遠(yuǎn)程連接Jupyter+pyspark部署教程
這篇文章主要為大家詳細(xì)介紹了Linux下遠(yuǎn)程連接Jupyter+pyspark部署教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06Python利用Matplotlib庫(kù)實(shí)現(xiàn)繪制餅形圖
這篇文章主要為大家分享了基于python+matplotlib庫(kù)的餅形圖繪制,具體內(nèi)容涉及一般的餅圖、分裂餅圖、以及環(huán)形圖,感興趣的小伙伴可以了解一下2022-04-04Python+OpenCV人臉識(shí)別簽到考勤系統(tǒng)實(shí)現(xiàn)(附demo)
本文主要介紹了Python+OpenCV人臉識(shí)別簽到考勤系統(tǒng)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04解決pytorch下只打印tensor的數(shù)值不打印出device等信息的問(wèn)題
這篇文章主要介紹了解決pytorch下只打印tensor的數(shù)值不打印出device等信息的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Python 將 QQ 好友頭像生成祝福語(yǔ)的實(shí)現(xiàn)代碼
這篇文章主要介紹了用 Python 將 QQ 好友頭像生成祝福語(yǔ)的實(shí)現(xiàn)代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05python 捕獲 shell/bash 腳本的輸出結(jié)果實(shí)例
下面小編就為大家?guī)?lái)一篇python 捕獲 shell/bash 腳本的輸出結(jié)果實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01pytorch 實(shí)現(xiàn)打印模型的參數(shù)值
今天小編就為大家分享一篇pytorch 實(shí)現(xiàn)打印模型的參數(shù)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12淺談算法之最小生成樹(shù)Kruskal的Python實(shí)現(xiàn)
最小生成樹(shù)Kruskal算法可以稱(chēng)為“加邊法”,初始最小生成樹(shù)邊數(shù)為0,每迭代一次就選擇一條滿(mǎn)足條件的最小代價(jià)邊,加入到最小生成樹(shù)的邊集合里。本文將介紹它的原理,并用Python進(jìn)行實(shí)現(xiàn)2021-06-06tensorflow構(gòu)建BP神經(jīng)網(wǎng)絡(luò)的方法
這篇文章主要為大家詳細(xì)介紹了tensorflow構(gòu)建BP神經(jīng)網(wǎng)絡(luò)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03