pyqt5读取excel表格显示在软件上代码

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>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值