当前位置: 首页 > news >正文

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

pip install pandas ;

pip install pyqt5; 
pip install pyqt5-tools; 

编写 pyqt5_read_etx.py 如下

# -*- 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 loadUiclass EtxReader(QMainWindow):def __init__(self):super(EtxReader, self).__init__()loadUi("readEtx.ui", self)  # Load the UI fileself.file_path = ""  # To store the file pathself.df = pd.DataFrame()  # To store the etx dataself.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_nameself.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.")returntry: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 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

http://www.lryc.cn/news/187980.html

相关文章:

  • 【QT】Windows 编译并使用 QT 5.12.7源码
  • php实战案例记录(15)获取GET和POST请求参数
  • k8s-9 ingress-nginx 特性
  • java案例24:模拟百度翻译
  • 汽车烟雾测漏仪(EP120)
  • 【轻松玩转MacOS】安全隐私篇
  • 4.02 用户中心-上传头像功能开发
  • 在Ubuntu 18.04安装Docker
  • Vue-2.1scoped样式冲突
  • Matlab之查询子字符串在字符串中的起始位置函数strfind
  • [游戏开发][Unity]安卓出包报错记录
  • 在两个有序数组中找整体第k小的数
  • Linux 指令心法(十)`head` 显示文本文件的开头部分
  • 前端——Layui的导航栏与tab页联动
  • 一致性哈希算法
  • 深度学习基础之参数量(3)
  • 红队专题-从零开始VC++远程控制软件RAT-C/S-[2]界面编写及上线
  • 磁盘满了对日志打印(Logback)的影响
  • 【算法与数据结构】--算法基础--数据结构概述
  • QECon大会亮相产品,全栈测试平台推荐:RunnerGo
  • 前端小案例-图片存放在远端服务器
  • 【鼠标右键菜单添加用VSCode打开文件或文件夹】
  • 【jvm--堆】
  • 【数据库——MySQL(实战项目1)】(1)图书借阅系统
  • [GXYCTF 2019]Ping Ping Ping题目解析
  • HTTP协议的请求协议和响应协议的组成,HTTP常见的状态信息
  • 【LeetCode】剑指 Offer Ⅱ 第6章:栈(6道题) -- Java Version
  • vue3的element-plus的el-dialog的样式修改无效问题
  • 归纳所猜半结论推出完整结论:CF1592F1
  • WPFdatagrid结合comboBox