Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to make Excel data packet aggregator with PyQt5

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use PyQt5 to make Excel data packet aggregator", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use PyQt5 to make Excel data packet aggregator" bar!

Sort out the requirements before writing the data summary grouping tool, requirement one: be able to display excel data in the list. Requirement 2: can support the summary of data by column, and multi-column group summary. Requirement 3: be able to preview the data after grouping summary, and finally save the grouped data to a new excel data file.

The following third-party python modules are mainly used. Unlike the previous PyQt5 applications, a style module qdarkstyle has been added this time, which can be shown as a cool black application by adding this module directly to QApplication. I personally prefer this style.

'Application Operation Library', 'import sysimport os''' Application style Library', 'from qdarkstyle import load_stylesheet_pyqt5''' UI Interface Library', 'from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import *' 'data extraction Library' 'import pandas as pd

Write UI interface component layout, UI layout function init_ui (). The whole content of the function of init_ui () is posted here, and the bosses can play as much as they want.

Def init_ui (self): # title, Icon setting self.setWindowTitle ('Excel data summary tool official account: [Python concentration camp]') self.setWindowIcon (QIcon (': / data_sum.ico')) # initialize horizontal layout hbox = QHBoxLayout () # initialize grid layout grid = QGridLayout () self.data_source_text = QLineEdit () self. Data_source_text.setReadOnly (True) self.data_source_btn = QPushButton () self.data_source_btn.setText ('data') self.data_source_btn.clicked.connect (self.data_source_btn_click) self.data_group_column = QLabel () self.data_group_column.setText ('set grouping column') self.data_group_column_text = QLineEdit () self.data_group_column_text.setPlaceholderText ('column name 1 List 2.') Self.save_dir_text = QLineEdit () self.save_dir_text.setReadOnly (True) self.save_dir_btn = QPushButton () self.save_dir_btn.setText ('path') self.save_dir_btn.clicked.connect (self.save_dir_btn_click) self.view_data_btn = QPushButton () self.view_data_btn.setText ('Preview According to') self.view_data_btn.clicked.connect (self.view_data_btn_click) self.save_data_btn = QPushButton () self.save_data_btn.setText ('save') 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,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,100) hbox.addWidget (self.table_view) hbox.addLayout (grid) self.setLayout (hbox)

A total of four slot functions are used, which are the following.

Save_data_btn_click: save the grouped DataFrame data directly.

Data_source_btn_click: used to load excel files that need to be grouped together, and display the loaded DataFrame data directly to the components of QTableView, so that the loaded raw data can be seen in real time.

Save_dir_btn_click: click the slot function triggered when the storage path is selected, which is used to call up QFileDialog to select the file path.

View_data_btn_click: bring up the data after previewing the grouped summary and display the grouped data on the window.

Slot function data_source_btn_click to load excel source data.

Def data_source_btn_click (self): xlsx_file = QFileDialog.getOpenFileName (self, 'Select File', 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)

Slot function save_data_btn_click to save the final excel data.

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=' data information summary')

The slot function view_data_btn_click, which previews the grouped data.

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)

Slot function save_dir_btn_click, which stores the file selection.

Def save_dir_btn_click (self): save_path = QFileDialog.getExistingDirectory (self, 'choose a folder', self.cwd) self.save_dir_text.setText (save_path +'/') Thank you for reading. This is the content of "how to use PyQt5 to make Excel data packet aggregator". After the study of this article I believe that you have a deeper understanding of how to use PyQt5 to make Excel data packet aggregator, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report