top | item 37228778

(no title)

fzumstein | 2 years ago

The same with xlwings (works on macOS, too):

  >>> import xlwings as xw
  >>> wb = xw.Book()
  >>> sh = wb.sheets[0]
  >>> sh["A1"].value = "Hello World"

discuss

order

jodrellblank|2 years ago

win32com.client uses Windows' COM machinery, which supports event-driven feedback from Excel to the script. Switching to PowerShell because I'm not familiar with that in Python and from a quick Google, Python needs some more complex multithreadding handling:

    $xl = new-object -ComObject excel.application

    $xl.Visible = $true
    
    $wb = $xl.WorkBooks.Add()
    $sh = $wb.Worksheets[1]
    
    
    Register-ObjectEvent -InputObject $xl -EventName SheetChange -Action {
        Write-Host "Hi"
    }
    
Now when you change a cell in the spreadsheet GUI and press enter, the PowerShell "write-host" runs and prints the message. Another comment mentioned "using Excel as a GUI for Python" and the COM interfaces have the machinery to do that. It looks like xlWings has something like this, but may need an add-in installed in Excel to trigger Python code from Excel? https://docs.xlwings.org/en/latest/quickstart.html and https://docs.xlwings.org/en/latest/addin.html#xlwings-addin