Здравствуйте, может кто подскажет как импортировать в QTableWidget (PyQt5) excel файл, который я ранее экспортировал из таблицы, буду благодарен за помощь
Python:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtCore import QSettings
from xlsxwriter.workbook import Workbook
class Ui_MainWindow(QtWidgets.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1920, 1000)
self.settings = QSettings("Nik", "Farmer.io", self)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.saveButton = QtWidgets.QPushButton(self.centralwidget)
self.saveButton.setGeometry(QtCore.QRect(5, 940, 90, 23))
self.saveButton.setObjectName("pushButton_save")
self.saveButton.clicked.connect(self.export)
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(200, 0, 1720, 1000))
self.loadButton = QtWidgets.QPushButton(self.centralwidget)
self.loadButton.setGeometry(QtCore.QRect(100, 940, 90, 23))
self.loadButton.setObjectName("pushButton_load")
self.loadButton.clicked.connect(self.addExcel)
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(200, 0, 1720, 1000))
Row = 37
self.row = Row
Col = 5
self.col = Col
self.tableWidget.setRowCount(Row)
self.tableWidget.setColumnCount(Col)
self.tableWidget.setObjectName("tableWidget")
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(4, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(5, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(6, item)
self.tableWidget.horizontalHeader().setDefaultSectionSize(237)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def export(self):
fileName, ok = QFileDialog.getSaveFileName(
self,
"Save File",
".",
"All Files(*.xlsx)"
)
if not fileName:
return
_list = []
model = self.tableWidget.model()
for row in range(model.rowCount()):
_r = []
for column in range(model.columnCount()):
_r.append("{}".format(model.index(row, column).data() or ""))
_list.append(_r)
print(fileName)
workbook = Workbook(fileName)
worksheet = workbook.add_worksheet()
for r, rows in enumerate(_list):
for c, cols in enumerate(rows):
worksheet.write(r, c, cols)
workbook.close()
msg = QMessageBox.information(
self,
"Success!",
f"Данные сохранены в файле: \n{fileName}"
)
def addExcel(self):
pass
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Последнее редактирование: