admin管理员组

文章数量:1287810

I used a simple method to load a table from Excel into the interface

    def getExcelFile(self):
        excel_file, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open file", downloads_path, "Excel files (*.xlsx)")
        if excel_file != "":
            self.mainui.excelFilePath.setText(excel_file)
            workbook = openpyxl.load_workbook(excel_file)
            worksheet = workbook.active
           self.mainui.excelTableOutput.setRowCount(worksheet.max_row)
            self.mainui.excelTableOutput.setColumnCount(worksheet.max_column)
            for i in range(worksheet.max_column):
                self.mainui.excelTableOutput.setColumnWidth(i, 170)
            self.mainui.excelTableOutput.horizontalHeader().setVisible(False)

            listValues = list(worksheet.values)

            self.mainui.excelStatusBar.setText("Loading Excel table")
            worksheet.max_column))
            rowIndex = 0
            for row in listValues:
                colIndex = 0
                for column in row:
                    self.mainui.excelTableOutput.setItem(rowIndex, colIndex, QtWidgets.QTableWidgetItem(column))
                    colIndex += 1
                rowIndex += 1
        else:
            self.mainui.excelFilePath.setText("Chose Excel file")

Thats work fine, but importing large tables takes a lot of time and the application freezes.

I decided to use multithreading and refactored code

class ExcelParser(QtCore.QObject):
    load_table = QtCore.pyqtSignal(str, int, int, int, int)
    @pyqtSlot(list, int, int)
    def loadExcelTableInThread(self, listValues, maxRow, maxColumn):
        rowIndex = 0
        for row in listValues:
            colIndex = 0
            for column in row:
                self.load_table.emit(column, rowIndex, colIndex, maxRow, maxColumn)
                colIndex += 1
            rowIndex += 1

class ZumParserWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.mainui = Ui_MainWindow()
        self.mainui.setupUi(self)
        # Creating worker thread
        self.ExcelParser = ExcelParser()
        self.ExcelThread = QtCore.QThread()
        self.ExcelParser.load_table.connect(self.loadExcelTableInThread)
        self.ExcelParser.moveToThread(self.ExcelThread)
        self.ExcelThread.start()

    # Get data from Excel file
    def getExcelFile(self):
        excel_file, _ = QtWidgets.QFileDialog.getOpenFileName(self, "Open file", downloads_path, "Excel files (*.xlsx)")
        if excel_file != "":
            self.mainui.excelFilePath.setText(excel_file)
            workbook = openpyxl.load_workbook(excel_file)
            worksheet = workbook.active
            self.mainui.excelTableOutput.horizontalHeader().setVisible(False)

            listValues = list(worksheet.values)

            self.mainui.excelStatusBar.setText("Loading Excel table")
            QtCore.QMetaObject.invokeMethod(self.ExcelParser, "loadExcelTableInThread",
                                            QtCore.Qt.ConnectionType.QueuedConnection,
                                            QtCore.Q_ARG(list, listValues),
                                            QtCore.Q_ARG(int,worksheet.max_row),
                                            QtCore.Q_ARG(int, worksheet.max_column))
        else:
            self.mainui.excelFilePath.setText("Chose Excel file")


    def loadExcelTableInThread(self, column, rowIndex, colIndex, maxRow, maxColumn):
        self.mainui.excelTableOutput.setRowCount(maxRow)
        self.mainui.excelTableOutput.setColumnCount(maxColumn)
        for i in range(maxColumn):
            self.mainui.excelTableOutput.setColumnWidth(i, 170)
        self.mainui.excelTableOutput.setItem(rowIndex, colIndex, QtWidgets.QTableWidgetItem(column))

This option works with some tables, but large files crash the application and in console i see

Process finished with exit code -1073740791 (0xC0000409)

I need multi-threaded loading to work with any Excel tables and not cause the application to crash. Otherwise, there is no point in it, since small tables do not cause freezes even without multithreading.

本文标签: pythonApp crash when im uploading big excel table using multithreadStack Overflow