微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何将 Excel 选定行和列中的值粘贴到 PYQT5 中的 Qtablewidget

如何解决如何将 Excel 选定行和列中的值粘贴到 PYQT5 中的 Qtablewidget

下面是我得到的代码供参考。在这里,我可以在应用程序中复制和粘贴选定的值。 但就我而言,我想在 excel 中复制某些单元格的值(假设为 A4:D6)并将其粘贴到此应用程序的 Qtableview 中。

我也尝试过 qclipboard 和其他选项,但没有奏效。它只是将所有单元格复制到应用程序中 Qtableview 的单元格中。但是应该和excel一样填写。

我几乎浏览了许多选项,但找不到任何解决方案。请支持

from PyQt5 import QtCore,QtGui,QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window,self).__init__()
        self.model = QtGui.QStandardItemmodel(10,10)
        self.tableView = QtWidgets.QTableView()
        self.tableView.setModel(self.model)

        self.tableView.installEventFilter(self)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.tableView)

        self.clipboard = []

    def eventFilter(self,source,event):
        if event.type() == QtCore.QEvent.KeyPress:
            if event == QtGui.QKeySequence.copy:
                self.copySelection()
                return True
            elif event == QtGui.QKeySequence.Paste:
                self.pasteSelection()
                return True
        elif event.type() == QtCore.QEvent.ContextMenu:
            # a context menu for the copy/paste operations
            menu = QtWidgets.QMenu()
            copyAction = menu.addAction('copy')
            copyAction.triggered.connect(self.copySelection)
            pasteAction = menu.addAction('Paste')
            pasteAction.triggered.connect(self.pasteSelection)
            if not self.tableView.selectedindexes():
                # no selection available,both copy and paste are disabled
                copyAction.setEnabled(False)
                pasteAction.setEnabled(False)
            if not self.clipboard:
                # no clipboard contents,paste is disabled
                pasteAction.setEnabled(False)
            menu.exec(event.globalPos())
            return True
        return super(Window,self).eventFilter(source,event)

    def copySelection(self):
        # clear the current contents of the clipboard
        self.clipboard.clear()
        selected = self.tableView.selectedindexes()
        rows = []
        columns = []
        # cycle all selected items to get the minimum row and column,so that the
        # reference will always be [0,0]
        for index in selected:
            rows.append(index.row())
            columns.append(index.column())
        minRow = min(rows)
        minCol = min(columns)
        for index in selected:
            # append the data of each selected index
            self.clipboard.append((index.row() - minRow,index.column() - minCol,index.data()))

    def pasteSelection(self):
        if not self.clipboard:
            return
        current = self.tableView.currentIndex()
        if not current.isValid():
            # in the rare case that there is no current index,use the first row
            # and column as target
            current = self.model.index(0,0)

        firstRow = current.row()
        firstColumn = current.column()

        # optional: get the selection model so that pasted indexes will be
        # automatically selected at the end
        selection = self.tableView.selectionModel()
        for row,column,data in self.clipboard:
            # get the index,with rows and columns relative to the current
            index = self.model.index(firstRow + row,firstColumn + column)
            # set the data for the index
            self.model.setData(index,data,QtCore.Qt.displayRole)
            # add the index to the selection
            selection.select(index,selection.Select)

        # apply the selection model
        self.tableView.setSelectionModel(selection)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())```

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。