欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于PyQt5制作Excel數(shù)據(jù)分組匯總器

 更新時(shí)間:2022年01月13日 08:32:32   作者:Python集中營(yíng)  
這篇文章主要介紹了基于PyQt5制作的一個(gè)小工具:Excel數(shù)據(jù)分組匯總器。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起試一試

在寫(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)文章

最新評(píng)論