網站首頁 編程語言 正文
在寫數據匯總分組工具之前梳理一下需求,要求一:能夠將excel的數據展示到列表中。要求二:能夠支持按列匯總數據,并且多列分組匯總。要求三:能夠預覽分組匯總以后的數據,最后將分好組匯總的數據保存到新的excel數據文件中。
主要使用到第三方python模塊有下面這些,和前面幾個 PyQt5 應用不同的是這次增加了一個樣式模塊 qdarkstyle ,通過最后將這個模塊直接加入到 QApplication 中就可以顯示成黑色酷酷的應用了。這個樣式我個人是比較喜歡的...
'''應用操作庫''' import sys import os '''應用樣式庫''' from qdarkstyle import load_stylesheet_pyqt5 '''UI界面庫''' from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * '''數據提取庫''' import pandas as pd
編寫 UI 界面組件布局,UI 布局函數 init_ui()。init_ui() 的函數整體內容都貼在下面這里,大佬們可以根據自己的需要隨意發揮。
def init_ui(self): # 標題、圖標設置 self.setWindowTitle('Excel數據匯總工具 公眾號:[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('數據') self.data_source_btn.clicked.connect(self.data_source_btn_click) self.data_group_column = QLabel() self.data_group_column.setText('設置分組列') 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('預覽數據') 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)
槽函數總共使用了四個,分別是下面這些。
save_data_btn_click:將分組匯總后的 DataFrame 數據直接保存。
data_source_btn_click:用來加載需要分組匯總的 excel 文件的,并將加載出來的 DataFrame 數據直接顯示到 QTableView 的組件上面,這樣可以實時的看見加載進來的原始數據。
save_dir_btn_click:點擊選擇存儲路徑時觸發的槽函數,用來調起 QFileDialog 來選擇文件路徑。
view_data_btn_click:調起預覽分組匯總后的數據,將分組后的數據顯示到窗口上。
槽函數 data_source_btn_click,加載 excel 源數據。
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)
槽函數 save_data_btn_click,保存最終的 excel 數據。
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='數據信息匯總')
槽函數 view_data_btn_click,預覽分組匯總的數據。
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)
槽函數 save_dir_btn_click,存儲文件選擇。
def save_dir_btn_click(self): save_path = QFileDialog.getExistingDirectory(self, '選擇文件夾', self.cwd) self.save_dir_text.setText(save_path + '/')
最后,在主要代碼塊中展示列表數據時時使用了一個 QTableView 組件自定義的模型 TableModelView。
原文鏈接:https://www.cnblogs.com/lwsbc/p/15795676.html
相關推薦
- 2022-12-28 jquery點擊獲取動態數據進行傳參問題_jquery
- 2022-01-19 標準時間格式轉換(正則寫法)
- 2022-08-22 Python基礎異常處理梳理總結_python
- 2022-12-27 Swift?Error重構的基礎示例詳解_Swift
- 2022-04-23 配置基于域名訪問的網站以及指定用戶可訪問的網站
- 2022-06-20 一文搞懂Go語言中條件語句的使用_Golang
- 2023-01-20 Python輸入圓半徑,計算圓周長和面積的實現方式_python
- 2022-03-16 使用Lvs+Nginx集群搭建高并發架構的實現示例_nginx
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支