使用 xlwings 运行 交互式 GUI 时发出问题

Issue while running interactive GUI with xlwings

我构建了一个 GUI(使用 PyQt5),它允许我读取 CSV,进行一些基本操作并将其发送到 Excel。

然后,我使用 xlwings 将此 GUI 集成到 Excel 中,但我遇到了问题。当我使用 GUI 时,我无法操作 Excel 中的数据。我想这是因为我的宏仍然是 运行ning。

有没有办法 运行 我的 GUI 而不会失去对 Excel 的控制?

def Main():
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

并在 Excel 中:

Sub GUI()
    RunPython ("import UImainwindow; UImainwindow.Main())
End sub

终于找到解决办法了

我修改了 ExecuteWindows 子程序以添加一个可选参数,如下所示:

Sub ExecuteWindows(IsFrozen As Boolean, PythonCommand As String, PYTHON_WIN As String, LOG_FILE As String, SHOW_LOG As Boolean, Optional PYTHONPATH As String, Optional WaitOnReturnBool As Boolean) 

然后我像这样修改 RunPython 函数

Public Function RunPython(PythonCommand As String, Optional ByVal WaitOnReturnBool As Boolean = True)

ExecuteWindows False, PythonCommand, PYTHON_WIN, LOG_FILE, SHOW_LOG, PYTHONPATH, WaitOnReturnBool

最后使用两个参数调用 RunPython 函数

RunPython command, WaitOnReturnBool

我不得不在我的脚本中使用 wb=xw.Workbook.active() 而不是 wb=xw.Workbook.caller() 但它有效。 这允许我 运行 外部 GUI 而不会失去对 Excel.

的控制