pyqt5:pandas 读取 Excel文件或 .etx 电子表格文件,并显示

pip install pandas ;

pip install pyqt5;

pip install pyqt5-tools;

编写 pyqt5_read_etx.py 如下

python 复制代码
# -*- coding: utf-8 -*-
""" pandas 读取 Excel文件或 .etx 电子表格文件,显示在 QTableWidget 中 """
import os
import sys
import numpy as np
import pandas as pd
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem
from PyQt5.uic import loadUi

class EtxReader(QMainWindow):

    def __init__(self):
        super(EtxReader, self).__init__()
        loadUi("readEtx.ui", self)  # Load the UI file
        self.file_path = ""  # To store the file path
        self.df = pd.DataFrame()  # To store the etx data
        self.browse_btn.clicked.connect(self.browse_file)
        self.read_btn.clicked.connect(self.read_etx)

    def browse_file(self):
        """ Open a file dialog to browse etx files.
        """
        file_name, _ = QFileDialog.getOpenFileName(self, "Open etx File", "",
                                "etx File(*.etx);;xlsx File(*.xlsx)")
        if file_name:
            self.file_path = file_name
            self.file_path_line_edit.setText(self.file_path)

    def read_etx(self):
        """ Read the etx file using Pandas and display the data in the table widget.
        """
        if not self.file_path:
            QMessageBox.critical(self, "Error", "Please select an etx 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(map(str,self.df.columns))
            #print(list(map(str,self.df.columns)))
            print('shape:', self.df.shape)
            for i in range(self.df.shape[0]):
                for j in range(self.df.shape[1]):
                    v = self.df.iloc[i, j]
                    #print(v)
                    if v is None or v is np.nan:
                        item = QTableWidgetItem('')
                    else:
                        item = QTableWidgetItem(str(v))
                    self.table_widget.setItem(i, j, item)
        except Exception as e:
            QMessageBox.critical(self, "Error", f"Error: {e}")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = EtxReader()
    window.show()
    sys.exit(app.exec_())

编写 readEtx.ui 如下

XML 复制代码
<?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>800</width>
    <height>600</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:</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="file_path_line_edit">
    <property name="geometry">
     <rect>
      <x>110</x>
      <y>20</y>
      <width>431</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>570</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>670</x>
      <y>20</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>60</y>
      <width>781</width>
      <height>501</height>
     </rect>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

运行 python pyqt5_read_etx.py

相关推荐
开开心心就好40 分钟前
高效Excel合并拆分软件
开发语言·javascript·c#·ocr·排序算法·excel·最小二乘法
Gyoku Mint21 小时前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib
沉到海底去吧Go21 小时前
【行驶证识别成表格】批量OCR行驶证识别与Excel自动化处理系统,行驶证扫描件和照片图片识别后保存为Excel表格,基于QT和华为ocr识别的实现教程
自动化·ocr·excel·行驶证识别·行驶证识别表格·批量行驶证读取表格
坚持就完事了1 天前
大二下期末
python·numpy·pandas
Abigail_chow2 天前
EXCEL如何快速批量给两字姓名中间加空格
windows·microsoft·excel·学习方法·政务
xiaohezi2 天前
Rag chunk 之:Excel 文档解析
excel
仟濹2 天前
「数据分析 - Pandas 函数」【数据分析全栈攻略:爬虫+处理+可视化+报告】
爬虫·数据分析·pandas
KENYCHEN奉孝2 天前
Pandas和Django的示例Demo
python·django·pandas
weixin_472339462 天前
python批量解析提取word内容到excel
python·word·excel
3 天前
Unity与Excel表格交互热更方案
unity·游戏引擎·excel