import pandas as pd
import numpy as np
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys
class 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(795, 605)
#MainWindow.setFixedSize(MainWindow.width(), MainWindow.height()) #禁止拉伸窗口大小
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.retranslateUi(MainWindow)
MainWindow.setCentralWidget(self.centralWidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.pushButton_6 = QtWidgets.QPushButton(self.centralWidget)
self.pushButton_6.setGeometry(QtCore.QRect(480, 487, 75, 23))
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_6.setText("列表打开")
self.pushButton_6.clicked.connect(self.open_excel)
self.tableWidget = QtWidgets.QTableWidget(self.centralWidget)
self.tableWidget.setGeometry(QtCore.QRect(10, 200, 772, 250))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.tableWidget.setFrameShape(QtWidgets.QFrame.NoFrame) #设置没有边框
self.tableWidget.setStyleSheet("selection-background-color:pink")
self.tableWidget.setEditTriggers(QAbstractItemView.DoubleClicked)
self.tableWidget.raise_()
self.tableWidget_1 = QtWidgets.QTableWidget(self.centralWidget)
self.tableWidget_1.setGeometry(QtCore.QRect(10, 124, 772, 76))
self.tableWidget_1.setObjectName("tableWidget_1")
self.tableWidget_1.setColumnCount(0)
self.tableWidget_1.setRowCount(0)
self.tableWidget_1.setEditTriggers(QAbstractItemView.NoEditTriggers)
self.tableWidget_1.setStyleSheet("selection-background-color:pink")
self.tableWidget_1.setFrameShape(QtWidgets.QFrame.NoFrame)
self.tableWidget_1.verticalHeader().setVisible(False) # 隐藏垂直表头
self.tableWidget_1.horizontalHeader().setVisible(False) # 隐藏水平表头
self.tableWidget_1.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) #隐藏垂直滚动条
self.tableWidget_1.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) #隐藏水平滚动条
###===========连接两个tablewidget的水平滚动条==========================================
self.tableWidget.horizontalScrollBar().valueChanged.connect(self.setValue)
def setValue(self):
self.tableWidget_1.horizontalScrollBar().setValue(self.tableWidget.horizontalScrollBar().value())
###===========连接两个tablewidget的水平滚动条==========================================
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "综合测试平台"))
def open_excel(self):
openfile_name = QFileDialog.getOpenFileName(self, '选择文件', '', 'Excel files(*.xlsx , *.xls)')
global path_openfile_name
path_openfile_name = openfile_name[0]
self.tableWidget_1.setColumnCount(12)
self.tableWidget_1.setRowCount(2)
###===========前面两个数字代表坐标,后面两个数字代表合并的行列数==========================================
self.tableWidget_1.setSpan(0, 0, 2, 1)
self.tableWidget_1.setSpan(0, 1, 2, 1)
self.tableWidget_1.setSpan(0, 2, 2, 1)
self.tableWidget_1.setSpan(0, 3, 2, 1)
self.tableWidget_1.setSpan(0, 4, 2, 1)
self.tableWidget_1.setSpan(0, 5, 2, 1)
self.tableWidget_1.setSpan(0, 6, 1, 3)
self.tableWidget_1.setSpan(0, 9, 1, 3)
###===========前面两个数字代表坐标,后面两个数字代表合并的行列数==========================================
newItem_1 = QTableWidgetItem("文件名")
self.tableWidget_1.setItem(0, 0, newItem_1)
newItem_1.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_2 = QTableWidgetItem("开关型号")
self.tableWidget_1.setItem(0, 1, newItem_2)
newItem_2.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_3 = QTableWidgetItem("开关编号")
self.tableWidget_1.setItem(0, 2, newItem_3)
newItem_3.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_4 = QTableWidgetItem("实验日期")
self.tableWidget_1.setItem(0, 3, newItem_4)
newItem_4.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_5 = QTableWidgetItem("操作类型")
self.tableWidget_1.setItem(0, 4, newItem_5)
newItem_5.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_6 = QTableWidgetItem("操作电压")
self.tableWidget_1.setItem(0, 5, newItem_6)
newItem_6.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_7 = QTableWidgetItem("合闸时间(ms)")
self.tableWidget_1.setItem(0, 6, newItem_7)
newItem_7.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_8 = QTableWidgetItem("分闸时间(ms)")
self.tableWidget_1.setItem(0, 9, newItem_8)
newItem_8.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_9 = QTableWidgetItem("A")
self.tableWidget_1.setItem(1, 6, newItem_9)
newItem_9.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_10 = QTableWidgetItem("B")
self.tableWidget_1.setItem(1, 7, newItem_10)
newItem_10.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_11 = QTableWidgetItem("C")
self.tableWidget_1.setItem(1, 8, newItem_11)
newItem_11.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_12 = QTableWidgetItem("A")
self.tableWidget_1.setItem(1, 9, newItem_12)
newItem_12.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_13 = QTableWidgetItem("B")
self.tableWidget_1.setItem(1, 10, newItem_13)
newItem_13.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
newItem_14 = QTableWidgetItem("C")
self.tableWidget_1.setItem(1, 11, newItem_14)
newItem_14.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
###===========读取表格,转换表格,==========================================
if len(path_openfile_name) > 0:
input_table_0 = pd.read_excel(path_openfile_name) # 读取excel文件里面的内容
input_table = input_table_0.fillna(value='') # 将pandas的显示值Nan转化为空
# print(input_table)
input_table_rows = input_table.shape[0] # 获取excel表格的行数,shape表示维度信息查看
input_table_colunms = input_table.shape[1] # 获取excel表格的列数
# print(input_table_rows)
# print(input_table_colunms)
#input_table_header = input_table.columns.tolist() # 获取excel数据的表头
#input_table_header = input_table.columns.values.tolist()
# print(input_table_header)
###===========读取表格,转换表格,============================================
###======================给tablewidget设置行列表头============================
self.tableWidget.setColumnCount(input_table_colunms) # 设置tableWidget的列数
self.tableWidget.setRowCount(input_table_rows) # 设置tableWidget的行数
#self.tableWidget.setHorizontalHeaderLabels(input_table_header) # 设置tableWidget的表头,setHorizontalHeaderLabels()设置水平标签
# self.tableWidget.setHorizontalHeaderLabels(['文件名','开关型号','开关编号','','',''])
self.tableWidget.verticalHeader().setVisible(False) # 隐藏垂直表头
self.tableWidget.horizontalHeader().setVisible(False) # 隐藏水平表头
###======================给tablewidget设置行列表头============================
###================遍历表格每个元素,同时添加到tablewidget中========================
for i in range(1,input_table_rows):
input_table_rows_values = input_table.iloc[[i]] # 读取excel表格每行数据
# print(input_table_rows_values)
input_table_rows_values_array = np.array(input_table_rows_values) # 将每行的数据以一维数组(矩阵)的形式存在
# print(input_table_rows_values_array)
input_table_rows_values_list = input_table_rows_values_array.tolist()[0] # 将excel表格每行的数据以列表形式储存
# print(input_table_rows_values_list)
for j in range(input_table_colunms):
input_table_items_list = input_table_rows_values_list[j] # 将excel表格每行数据依次排序
# print(input_table_items_list)
# print(type(input_table_items_list))
###==============将遍历的元素添加到tablewidget中并显示=======================
input_table_items = str(input_table_items_list)
# print(input_table_items)
newItem = QTableWidgetItem(input_table_items)
newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter) # 设置单元格内文本的对齐方式,居中显示在垂直或者水平向上
self.tableWidget.setItem(i, j, newItem) # 设置表格中的值
###================遍历表格每个元素,同时添加到tablewidget中========================
self.tableWidget.setRowHidden(0, 1) #隐藏tableWidget中指定的行,0代表行数,1代表布尔值
else:
self.centralWidget.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
my = Ui_MainWindow()
my.show()
sys.exit(app.exec_())