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

2023-10-11 00:36

本文主要是介绍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

这篇关于pyqt5:pandas 读取 Excel文件或 .etx 电子表格文件,并显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/184239

相关文章

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

Java Web实现类似Excel表格锁定功能实战教程

《JavaWeb实现类似Excel表格锁定功能实战教程》本文将详细介绍通过创建特定div元素并利用CSS布局和JavaScript事件监听来实现类似Excel的锁定行和列效果的方法,感兴趣的朋友跟随... 目录1. 模拟Excel表格锁定功能2. 创建3个div元素实现表格锁定2.1 div元素布局设计2.

SpringSecurity显示用户账号已被锁定的原因及解决方案

《SpringSecurity显示用户账号已被锁定的原因及解决方案》SpringSecurity中用户账号被锁定问题源于UserDetails接口方法返回值错误,解决方案是修正isAccountNon... 目录SpringSecurity显示用户账号已被锁定的解决方案1.问题出现前的工作2.问题出现原因各

从基础到进阶详解Pandas时间数据处理指南

《从基础到进阶详解Pandas时间数据处理指南》Pandas构建了完整的时间数据处理生态,核心由四个基础类构成,Timestamp,DatetimeIndex,Period和Timedelta,下面我... 目录1. 时间数据类型与基础操作1.1 核心时间对象体系1.2 时间数据生成技巧2. 时间索引与数据

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

Python中文件读取操作漏洞深度解析与防护指南

《Python中文件读取操作漏洞深度解析与防护指南》在Web应用开发中,文件操作是最基础也最危险的功能之一,这篇文章将全面剖析Python环境中常见的文件读取漏洞类型,成因及防护方案,感兴趣的小伙伴可... 目录引言一、静态资源处理中的路径穿越漏洞1.1 典型漏洞场景1.2 os.path.join()的陷

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

基于Python+PyQt5打造一个跨平台Emoji表情管理神器

《基于Python+PyQt5打造一个跨平台Emoji表情管理神器》在当今数字化社交时代,Emoji已成为全球通用的视觉语言,本文主要为大家详细介绍了如何使用Python和PyQt5开发一个功能全面的... 目录概述功能特性1. 全量Emoji集合2. 智能搜索系统3. 高效交互设计4. 现代化UI展示效果