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

相关文章

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

使用Java读取本地文件并转换为MultipartFile对象的方法

《使用Java读取本地文件并转换为MultipartFile对象的方法》在许多JavaWeb应用中,我们经常会遇到将本地文件上传至服务器或其他系统的需求,在这种场景下,MultipartFile对象非... 目录1. 基本需求2. 自定义 MultipartFile 类3. 实现代码4. 代码解析5. 自定

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

MySQL 数据库表操作完全指南:创建、读取、更新与删除实战

《MySQL数据库表操作完全指南:创建、读取、更新与删除实战》本文系统讲解MySQL表的增删查改(CURD)操作,涵盖创建、更新、查询、删除及插入查询结果,也是贯穿各类项目开发全流程的基础数据交互原... 目录mysql系列前言一、Create(创建)并插入数据1.1 单行数据 + 全列插入1.2 多行数据

pandas数据的合并concat()和merge()方式

《pandas数据的合并concat()和merge()方式》Pandas中concat沿轴合并数据框(行或列),merge基于键连接(内/外/左/右),concat用于纵向或横向拼接,merge用于... 目录concat() 轴向连接合并(1) join='outer',axis=0(2)join='o

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3