pyqt5读取excel表格并显示
程序执行效果如下
python代码如下
import sys
import pandas as pd
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox, QTableWidgetItem
from PyQt5.uic import loadUi
class ExcelReader(QMainWindow):
def __init__(self):
super(ExcelReader, self).__init__()
loadUi("ReadExcel.ui", self) # Load the UI file
self.file_path = "" # To store the file path
self.df = pd.DataFrame() # To store the Excel data
self.browse_btn.clicked.connect(self.browse_file)
self.read_btn.clicked.connect(self.read_excel)
def browse_file(self):
"""
Open a file dialog to browse Excel files.
"""
file_name, _ = QFileDialog.getOpenFileName(self, "Open Excel File", "", "Excel Files (*.xlsx)")
if file_name:
self.file_path = file_name
self.file_path_line_edit.setText(self.file_path)
def read_excel(self):
"""
Read the Excel file using Pandas and display the data in the table widget.
"""
if not self.file_path:
QMessageBox.critical(self, "Error", "Please select an Excel file to read.")
return
try:
self.df = pd.read_excel(self.file_path)
self.table_widget.setRowCount(self.df.shape[0])
self.table_widget.setColumnCount(self.df.shape[1])
self.table_widget.setHorizontalHeaderLabels(self.df.columns)
for i in range(self.df.shape[0]):
for j in range(self.df.shape[1]):
self.table_widget.setItem(i, j, QTableWidgetItem(str(self.df.iloc[i, j])))
except Exception as e:
QMessageBox.critical(self, "Error", f"An error occurred while reading the Excel file.\n\n{e}")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ExcelReader()
window.show()
sys.exit(app.exec_())
UI代码如下
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ExcelReader</class>
<widget class="QMainWindow" name="ExcelReader">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="windowTitle">
<string>Excel Reader</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="file_path_label">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>111</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Excel File Path:</string>
</property>
</widget>
<widget class="QLineEdit" name="file_path_line_edit">
<property name="geometry">
<rect>
<x>120</x>
<y>20</y>
<width>421</width>
<height>22</height>
</rect>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
<widget class="QPushButton" name="browse_btn">
<property name="geometry">
<rect>
<x>550</x>
<y>20</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Browse</string>
</property>
</widget>
<widget class="QPushButton" name="read_btn">
<property name="geometry">
<rect>
<x>10</x>
<y>60</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>Read</string>
</property>
</widget>
<widget class="QTableWidget" name="table_widget">
<property name="geometry">
<rect>
<x>10</x>
<y>100</y>
<width>621</width>
<height>351</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>