Use the relevant Pyqt learned to realize the processing of the internal data of the Excel file. First, you need to obtain the data of Excel, and then save and process it to complete the corresponding operation.
def save_data_btn_click(self):
dir = self.save_dir_text.text().strip()
self.data_frame_group.to_excel(dir + 'output.xlsx',sheet_name='datasum')
This is a Python function named
save_data_btn_click
, and its function is to save the data to the specified file. The function accepts two parameters:self
apandas
data frame object used to save the data;dir
a string used to specify the path to save the file.In the function, first save the data frame to the specified file by
self.save_dir_text.text().strip()
getting the path to save the file, and then useself.data_frame_group.to_excel(dir + 'output.xlsx',sheet_name='datasum')
It should be noted that when using
pandas
a library, you need to install the library first and add it to Python's module search path.
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)
Here's a Python function called
view_data_btn_click
, and what it does is display the data in the data frame. The function accepts two parameters:self
apandas
data frame object, used to display the data;columns
and a string, used to specify the column names to display.In the function, first by
self.data_group_column_text.text().strip()
getting the column names to display, then use toself.data_frame_group.groupby(column_list, as_index=False).sum()
group the data frame by the column names, and use toprint(self.data_frame_group)
print the grouped data.Next, create an
TableModelView
object and pass in the grouped data frame as a parameter. Finally, use a modelself.table_view.setModel(model)
thatTableModelView
sets the object as a data frame.
def save_dir_btn_click(self):
save_path = QFileDialog.getExistingDirectory(self, 'chooselab', self.cwd)
self.save_dir_text.setText(save_path + '/')
Here's a Python function called
save_dir_btn_click
, and what it does is take the path of the directory where the file is saved from the user and save it into a string variable. This function accepts two parameters:self
anQFileDialog
object used to obtain the directory path selected by the user;save_path
a string variable used to save the directory path selected by the user.In the function, first use
QFileDialog.getExistingDirectory()
the method to get the directory path selected by the user and save it tosave_path
a variable. Then,save_path
set the variable asQFileDialog.getExistingDirectory()
the return value of the method so that the next time the function is called, it will be able to get the directory path selected by the user.
def data_source_btn_click(self):
xlsx_file = QFileDialog.getOpenFileName(self, 'choosetxt', 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)
Here's a Python function called
data_source_btn_click
, and what it does is take the filename of the Excel file from the user and save it into a string variable. This function accepts two parameters:self
anQFileDialog
object used to obtain the file path selected by the user;xlsx_file
a string variable used to save the file path selected by the user.In the function, first use
QFileDialog.getOpenFileName()
the method to get the file path selected by the user and save it toxlsx_file
a variable. Then,xlsx_file
set the variable asQFileDialog.getOpenFileName()
the return value of the method so that the next time the function is called, it will be able to get the file path selected by the user.Next, use
pd.read_excel()
the method to read the Excel file and save it toself.data_frame
a variable. Finally, anTableModelView
object is created andself.data_frame
the variables are passed in as parameters. Finally, use a modelself.table_view.setModel(model)
thatTableModelView
sets the object as a data frame.
Summary:
import sys
import os
from qdarkstyle import load_stylesheet_pyqt5
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import pandas as pd
class TableModelView(QAbstractTableModel):
def __init__(self, data):
QAbstractTableModel.__init__(self)
self._data = data
def rowCount(self, parent=None):
return self._data.shape[0]
def columnCount(self, parent=None):
return self._data.shape[1]
def data(self, index, role=Qt.DisplayRole):
if index.isValid():
if role == Qt.DisplayRole:
return str(self._data.iloc[index.row(), index.column()])
return None
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[col]
return None
class DataGroupSum(QWidget):
def __init__(self):
super(DataGroupSum, self).__init__()
self.cwd = os.getcwd()
self.init_ui()
def init_ui(self):
# 标题、图标设置
self.setWindowTitle('Excel data lab')
# 初始化水平布局
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('data')
self.data_source_btn.clicked.connect(self.data_source_btn_click)
self.data_group_column = QLabel()
self.data_group_column.setText('setlab')
self.data_group_column_text = QLineEdit()
self.data_group_column_text.setPlaceholderText('line1,line2...')
self.save_dir_text = QLineEdit()
self.save_dir_text.setReadOnly(True)
self.save_dir_btn = QPushButton()
self.save_dir_btn.setText('road')
self.save_dir_btn.clicked.connect(self.save_dir_btn_click)
self.view_data_btn = QPushButton()
self.view_data_btn.setText('preview')
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, 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(400)
hbox.addWidget(self.table_view)
hbox.addLayout(grid)
self.setLayout(hbox)
def data_source_btn_click(self):
xlsx_file = QFileDialog.getOpenFileName(self, 'choosetxt', 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)
def save_dir_btn_click(self):
save_path = QFileDialog.getExistingDirectory(self, 'chooselab', self.cwd)
self.save_dir_text.setText(save_path + '/')
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)
def save_data_btn_click(self):
dir = self.save_dir_text.text().strip()
self.data_frame_group.to_excel(dir + 'output.xlsx',sheet_name='datasum')
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setStyleSheet(load_stylesheet_pyqt5())
main = DataGroupSum()
main.show()
sys.exit(app.exec_())