日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pyqt 打开并显示excel表单

發布時間:2024/1/8 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pyqt 打开并显示excel表单 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考鏈接:Python-PyQt5-圖形可視化界面(7)-打開表格并顯示內容 - 簡書

from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import * from PyQt5.QtCore import * import pandas as pd import numpy as npclass Ui_MainWindow(QMainWindow):def __init__(self):super(QtWidgets.QMainWindow,self).__init__()self.setupUi(self)self.retranslateUi(self)def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(666, 488)self.centralWidget = QtWidgets.QWidget(MainWindow)self.centralWidget.setObjectName("centralWidget")self.retranslateUi(MainWindow)self.tableWidget = QtWidgets.QTableWidget(self.centralWidget)self.tableWidget.setGeometry(QtCore.QRect(0, 60, 813, 371))self.tableWidget.setObjectName("tableWidget")self.tableWidget.setColumnCount(0)self.tableWidget.setRowCount(0)self.tableWidget.setStyleSheet("selection-background-color:pink")self.tableWidget.setEditTriggers(QAbstractItemView.NoEditTriggers)self.tableWidget.raise_()self.pushButton = QtWidgets.QPushButton(self.centralWidget)self.pushButton.setGeometry(QtCore.QRect(90, 20, 75, 23))self.pushButton.setObjectName("pushButton")self.pushButton.setText("打開")MainWindow.setCentralWidget(self.centralWidget)QtCore.QMetaObject.connectSlotsByName(MainWindow)self.pushButton.clicked.connect(self.openfile)self.pushButton.clicked.connect(self.creat_table_show)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "一顆數據小白菜"))def openfile(self):###獲取路徑===================================================================openfile_name = QFileDialog.getOpenFileName(self,'選擇文件','','Excel files(*.xlsx , *.xls)')#print(openfile_name)global path_openfile_name###獲取路徑====================================================================path_openfile_name = openfile_name[0]def creat_table_show(self):###===========讀取表格,轉換表格,===========================================if len(path_openfile_name) > 0:input_table = pd.read_excel(path_openfile_name)#print(input_table)input_table_rows = input_table.shape[0]input_table_colunms = input_table.shape[1]#print(input_table_rows)#print(input_table_colunms)input_table_header = input_table.columns.values.tolist()#print(input_table_header)###===========讀取表格,轉換表格,============================================###======================給tablewidget設置行列表頭============================self.tableWidget.setColumnCount(input_table_colunms)self.tableWidget.setRowCount(input_table_rows)self.tableWidget.setHorizontalHeaderLabels(input_table_header)###======================給tablewidget設置行列表頭============================###================遍歷表格每個元素,同時添加到tablewidget中========================for i in range(input_table_rows):input_table_rows_values = input_table.iloc[[i]]#print(input_table_rows_values)input_table_rows_values_array = np.array(input_table_rows_values)input_table_rows_values_list = input_table_rows_values_array.tolist()[0]#print(input_table_rows_values_list)for j in range(input_table_colunms):input_table_items_list = input_table_rows_values_list[j]#print(input_table_items_list)# print(type(input_table_items_list))###==============將遍歷的元素添加到tablewidget中并顯示=======================input_table_items = str(input_table_items_list)newItem = QTableWidgetItem(input_table_items) newItem.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter)self.tableWidget.setItem(i, j, newItem) ###================遍歷表格每個元素,同時添加到tablewidget中========================else:self.centralWidget.show()if __name__ == "__main__":import sysapp = QtWidgets.QApplication(sys.argv)MainWindow = QtWidgets.QMainWindow()ui = Ui_MainWindow()ui.setupUi(MainWindow)MainWindow.show()sys.exit(app.exec_())

總結

以上是生活随笔為你收集整理的pyqt 打开并显示excel表单的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。