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


    pip install pandas ;

    pip install pyqt5
    pip install pyqt5-tools; 

    编写 pyqt5_read_etx.py 如下

    1. # -*- coding: utf-8 -*-
    2. """ pandas 读取 Excel文件或 .etx 电子表格文件,显示在 QTableWidget 中 """
    3. import os
    4. import sys
    5. import numpy as np
    6. import pandas as pd
    7. from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox
    8. from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem
    9. from PyQt5.uic import loadUi
    10. class EtxReader(QMainWindow):
    11. def __init__(self):
    12. super(EtxReader, self).__init__()
    13. loadUi("readEtx.ui", self) # Load the UI file
    14. self.file_path = "" # To store the file path
    15. self.df = pd.DataFrame() # To store the etx data
    16. self.browse_btn.clicked.connect(self.browse_file)
    17. self.read_btn.clicked.connect(self.read_etx)
    18. def browse_file(self):
    19. """ Open a file dialog to browse .etx file
    20. """
    21. file_name, _ = QFileDialog.getOpenFileName(self, "Open etx File", "",
    22. "xlsx File(*.xlsx);;etx File(*.etx)")
    23. if file_name:
    24. self.file_path = file_name
    25. self.file_path_line_edit.setText(self.file_path)
    26. def read_etx(self):
    27. """ Read .etx file using Pandas and display the data in the table widget.
    28. """
    29. if not self.file_path:
    30. QMessageBox.critical(self, "Error", "Please select an etx file to read.")
    31. return
    32. try:
    33. self.df = pd.read_excel(self.file_path)
    34. self.table_widget.setRowCount(self.df.shape[0])
    35. self.table_widget.setColumnCount(self.df.shape[1])
    36. self.table_widget.setHorizontalHeaderLabels(map(str, self.df.columns))
    37. #print(list(map(str, self.df.columns)))
    38. print('shape:', self.df.shape)
    39. for i in range(self.df.shape[0]):
    40. for j in range(self.df.shape[1]):
    41. v = self.df.iloc[i, j]
    42. #print(type(v), v)
    43. if (v is None) or (v is np.nan):
    44. item = QTableWidgetItem('')
    45. elif type(v) is str:
    46. item = QTableWidgetItem(v)
    47. elif type(v) is int or type(v) is np.int64:
    48. item = QTableWidgetItem("%d" %v)
    49. elif type(v) is float or type(v) is np.float64:
    50. item = QTableWidgetItem("%.4f" %v)
    51. else:
    52. #print(type(v), v)
    53. item = QTableWidgetItem(str(v))
    54. self.table_widget.setItem(i, j, item)
    55. except Exception as e:
    56. QMessageBox.critical(self, "Error", f"Error: {e}")
    57. if __name__ == "__main__":
    58. app = QApplication(sys.argv)
    59. window = EtxReader()
    60. window.show()
    61. sys.exit(app.exec_())

    编写 readEtx.ui 如下

    1. "1.0" encoding="UTF-8"?>
    2. <ui version="4.0">
    3. <class>ExcelReaderclass>
    4. <widget class="QMainWindow" name="ExcelReader">
    5. <property name="geometry">
    6. <rect>
    7. <x>0x>
    8. <y>0y>
    9. <width>800width>
    10. <height>600height>
    11. rect>
    12. property>
    13. <property name="windowTitle">
    14. <string>Excel Readerstring>
    15. property>
    16. <widget class="QWidget" name="centralwidget">
    17. <widget class="QLabel" name="file_path_label">
    18. <property name="geometry">
    19. <rect>
    20. <x>10x>
    21. <y>20y>
    22. <width>111width>
    23. <height>16height>
    24. rect>
    25. property>
    26. <property name="text">
    27. <string>Excel File:string>
    28. property>
    29. widget>
    30. <widget class="QLineEdit" name="file_path_line_edit">
    31. <property name="geometry">
    32. <rect>
    33. <x>110x>
    34. <y>20y>
    35. <width>431width>
    36. <height>22height>
    37. rect>
    38. property>
    39. <property name="readOnly">
    40. <bool>truebool>
    41. property>
    42. widget>
    43. <widget class="QPushButton" name="browse_btn">
    44. <property name="geometry">
    45. <rect>
    46. <x>570x>
    47. <y>20y>
    48. <width>75width>
    49. <height>23height>
    50. rect>
    51. property>
    52. <property name="text">
    53. <string>Browsestring>
    54. property>
    55. widget>
    56. <widget class="QPushButton" name="read_btn">
    57. <property name="geometry">
    58. <rect>
    59. <x>670x>
    60. <y>20y>
    61. <width>75width>
    62. <height>23height>
    63. rect>
    64. property>
    65. <property name="text">
    66. <string>Readstring>
    67. property>
    68. widget>
    69. <widget class="QTableWidget" name="table_widget">
    70. <property name="geometry">
    71. <rect>
    72. <x>10x>
    73. <y>60y>
    74. <width>781width>
    75. <height>501height>
    76. rect>
    77. property>
    78. widget>
    79. widget>
    80. <widget class="QMenuBar" name="menubar">
    81. <property name="geometry">
    82. <rect>
    83. <x>0x>
    84. <y>0y>
    85. <width>800width>
    86. <height>26height>
    87. rect>
    88. property>
    89. widget>
    90. <widget class="QStatusBar" name="statusbar"/>
    91. widget>
    92. <resources/>
    93. <connections/>
    94. ui>

    运行 python pyqt5_read_etx.py

  • 相关阅读:
    C++笔记 13 (STL初识)
    点云深度学习——点云配准网络DCP复现
    Vue 商场首页头部布局
    数据结构-顺序存储二叉树
    JVM垃圾回收机制
    istio pod不启动及访问报RBAC错误问题解决
    时序预测 | MATLAB实现PSO-LSSVM粒子群算法优化最小二乘支持向量机时间序列预测未来
    [Unity2D独立/合作开发]实现记录物品在不同场景的存在状态,附:场景的淡入淡出功能和预加载
    【Android进阶】6、多 Activity 间通信
    java计算机毕业设计ssm陕理工图书馆管理系统
  • 原文地址:https://blog.csdn.net/belldeep/article/details/133756391