PySide6读取EXCLE文档
作者:互联网
1、新建一个run.py(要运行的文件),代码如下
# -*- coding: utf-8 -*- """ 文件名称: run.py 文件创建时间: 2021/8/10 11:08 软件名称: PyCharm 文件作者: 黄氏 """ import sys from PySide6 import QtCore from PySide6.QtWidgets import QApplication import pandas as mypd # 导入所画的界面 from tabledemo import * # 自定义类 class Excel_Table(QMainWindow, Ui_MainWindow): def __init__(self): super().__init__() self.setupUi(self) self.excelButton.clicked.connect(self.Open_Excel_File) # connect作用就是执行某个函数 self.saveButton.clicked.connect(self.Excel_Save) def Open_Excel_File(self): excel_name = QFileDialog.getOpenFileName(self, '选择文件', '', 'Excel files(*.xlsx)') # 过滤出xlsx格式的文件 self.path_excel_name = excel_name[0] print("Excel文档的路径: ", self.path_excel_name) if len(self.path_excel_name) > 0: self.Read_Excel_Data() self.Table_Initialize() # 读取 EXCEL 文档的数据 def Read_Excel_Data(self): self.excel_file = mypd.read_excel(self.path_excel_name) # 获取数据的行标签与列标签 self.columns = self.excel_file.columns self.rows = self.excel_file.index # 定义表格的初始化参数 def Table_Initialize(self): self.tableWidget.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) self.tableWidget.setRowCount(len(self.rows)) # 设置表格的行数 self.tableWidget.setColumnCount(len(self.columns)) # 设置表格的列数 self.tableWidget.setHorizontalHeaderLabels(self.columns) # 设置表格的列标签 self.tableWidget.setGeometry(QtCore.QRect(223,120,1000,800)) # 重新设置表格区域的大小 # 将 EXCEL 文档的数据显示在表格中 for i in range(len(self.rows)): for j in range(len(self.columns)): tstr = str(self.excel_file.iloc[i][j]) data = QTableWidgetItem(tstr) self.tableWidget.setItem(i, j, data) self.tableWidget.resizeColumnsToContents() # 列宽随着内容调整 self.tableWidget.resizeRowsToContents() # 行宽随着内容调整 self.tableWidget.setAlternatingRowColors(True) # 表格的颜色交错显示 self.tableWidget.itemChanged.connect(self.Table_Data_Change) # 将表格中单元格改变时,触发 Table_Data_Change 事件 # 当表格的内容改变时获取内容 def Table_Data_Change(self,item): text = item.text() itemrow = item.row() itemcol = item.column() self.excel_file.iloc[itemrow,itemcol] = text # 使用[i][j]形式会报销
def Excel_Save(self): self.excel_file.to_excel(self.path_excel_name, index=False) if __name__ == '__main__': app = QApplication(sys.argv) excelTable = Excel_Table() excelTable.show() sys.exit(app.exec())
2、新建一个ui的python文档,文件 tabledemo.py 代码如下(我是用QtDesigner设计,通过PyUIC命令转换过来的)
# -*- coding: utf-8 -*- ################################################################################ ## Form generated from reading UI file 'tabledemo.ui' ## ## Created by: Qt User Interface Compiler version 6.1.2 ## ## WARNING! All changes made in this file will be lost when recompiling UI file! ################################################################################ from PySide6.QtCore import * # type: ignore from PySide6.QtGui import * # type: ignore from PySide6.QtWidgets import * # type: ignore class Ui_MainWindow(object): def setupUi(self, MainWindow): if not MainWindow.objectName(): MainWindow.setObjectName(u"MainWindow") MainWindow.resize(918, 538) sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth()) MainWindow.setSizePolicy(sizePolicy) MainWindow.setLayoutDirection(Qt.LeftToRight) self.centralwidget = QWidget(MainWindow) self.centralwidget.setObjectName(u"centralwidget") self.formLayout = QFormLayout(self.centralwidget) self.formLayout.setObjectName(u"formLayout") self.horizontalLayout = QHBoxLayout() self.horizontalLayout.setObjectName(u"horizontalLayout") self.excelButton = QPushButton(self.centralwidget) self.excelButton.setObjectName(u"excelButton") self.horizontalLayout.addWidget(self.excelButton) self.horizontalSpacer = QSpacerItem(808, 17, QSizePolicy.Expanding, QSizePolicy.Minimum) self.horizontalLayout.addItem(self.horizontalSpacer) self.formLayout.setLayout(0, QFormLayout.LabelRole, self.horizontalLayout) self.horizontalLayout_2 = QHBoxLayout() self.horizontalLayout_2.setObjectName(u"horizontalLayout_2") self.horizontalSpacer_2 = QSpacerItem(828, 18, QSizePolicy.Expanding, QSizePolicy.Minimum) self.horizontalLayout_2.addItem(self.horizontalSpacer_2) self.saveButton = QPushButton(self.centralwidget) self.saveButton.setObjectName(u"saveButton") sizePolicy1 = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) sizePolicy1.setHorizontalStretch(0) sizePolicy1.setVerticalStretch(0) sizePolicy1.setHeightForWidth(self.saveButton.sizePolicy().hasHeightForWidth()) self.saveButton.setSizePolicy(sizePolicy1) self.saveButton.setContextMenuPolicy(Qt.DefaultContextMenu) self.saveButton.setLayoutDirection(Qt.LeftToRight) self.horizontalLayout_2.addWidget(self.saveButton) self.formLayout.setLayout(2, QFormLayout.LabelRole, self.horizontalLayout_2) self.tableWidget = QTableWidget(self.centralwidget) self.tableWidget.setObjectName(u"tableWidget") self.tableWidget.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents) self.formLayout.setWidget(1, QFormLayout.SpanningRole, self.tableWidget) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QMenuBar(MainWindow) self.menubar.setObjectName(u"menubar") self.menubar.setGeometry(QRect(0, 0, 918, 22)) MainWindow.setMenuBar(self.menubar) self.statusbar = QStatusBar(MainWindow) self.statusbar.setObjectName(u"statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QMetaObject.connectSlotsByName(MainWindow) # setupUi def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None)) self.excelButton.setText(QCoreApplication.translate("MainWindow", u"\u6253\u5f00Excel", None)) self.saveButton.setText(QCoreApplication.translate("MainWindow", u"\u4fdd \u5b58", None)) # retranslateUi
标签:horizontalLayout,QSizePolicy,self,excel,PySide6,tableWidget,文档,EXCLE,MainWindow 来源: https://www.cnblogs.com/yimai-series/p/15129042.html