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

相关文章

第10章 中断和动态时钟显示

第10章 中断和动态时钟显示 从本章开始,按照书籍的划分,第10章开始就进入保护模式(Protected Mode)部分了,感觉从这里开始难度突然就增加了。 书中介绍了为什么有中断(Interrupt)的设计,中断的几种方式:外部硬件中断、内部中断和软中断。通过中断做了一个会走的时钟和屏幕上输入字符的程序。 我自己理解中断的一些作用: 为了更好的利用处理器的性能。协同快速和慢速设备一起工作

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

pandas数据过滤

Pandas 数据过滤方法 Pandas 提供了多种方法来过滤数据,可以根据不同的条件进行筛选。以下是一些常见的 Pandas 数据过滤方法,结合实例进行讲解,希望能帮你快速理解。 1. 基于条件筛选行 可以使用布尔索引来根据条件过滤行。 import pandas as pd# 创建示例数据data = {'Name': ['Alice', 'Bob', 'Charlie', 'Dav

matlab读取NC文件(含group)

matlab读取NC文件(含group): NC文件数据结构: 代码: % 打开 NetCDF 文件filename = 'your_file.nc'; % 替换为你的文件名% 使用 netcdf.open 函数打开文件ncid = netcdf.open(filename, 'NC_NOWRITE');% 查看文件中的组% 假设我们想读取名为 "group1" 的组groupName

lvgl8.3.6 控件垂直布局 label控件在image控件的下方显示

在使用 LVGL 8.3.6 创建一个垂直布局,其中 label 控件位于 image 控件下方,你可以使用 lv_obj_set_flex_flow 来设置布局为垂直,并确保 label 控件在 image 控件后添加。这里是如何步骤性地实现它的一个基本示例: 创建父容器:首先创建一个容器对象,该对象将作为布局的基础。设置容器为垂直布局:使用 lv_obj_set_flex_flow 设置容器

C# dateTimePicker 显示年月日,时分秒

dateTimePicker默认只显示日期,如果需要显示年月日,时分秒,只需要以下两步: 1.dateTimePicker1.Format = DateTimePickerFormat.Time 2.dateTimePicker1.CustomFormat = yyyy-MM-dd HH:mm:ss Tips:  a. dateTimePicker1.ShowUpDown = t

C#关闭指定时间段的Excel进程的方法

private DateTime beforeTime;            //Excel启动之前时间          private DateTime afterTime;               //Excel启动之后时间          //举例          beforeTime = DateTime.Now;          Excel.Applicat

excel翻译软件有哪些?如何高效提翻译?

你是否曾在面对满屏的英文Excel表格时感到头疼?项目报告、数据分析、财务报表... 当这些重要的信息被语言壁垒阻挡时,效率和理解度都会大打折扣。别担心,只需3分钟,我将带你轻松解锁excel翻译成中文的秘籍。 无论是职场新人还是老手,这一技巧都将是你的得力助手,让你在信息的海洋中畅游无阻。 方法一:使用同声传译王软件 同声传译王是一款专业的翻译软件,它支持多种语言翻译,可以excel

argodb自定义函数读取hdfs文件的注意点,避免FileSystem已关闭异常

一、问题描述 一位同学反馈,他写的argo存过中调用了一个自定义函数,函数会加载hdfs上的一个文件,但有些节点会报FileSystem closed异常,同时有时任务会成功,有时会失败。 二、问题分析 argodb的计算引擎是基于spark的定制化引擎,对于自定义函数的调用跟hive on spark的是一致的。udf要通过反射生成实例,然后迭代调用evaluate。通过代码分析,udf在