XLwings 在 ListBox 上选择项目

XLwings selecting item on ListBox

我正在尝试从列表框中以编程方式 select 项目。在 VBA 我会做:

Sheet1.MyListBox.Selected(0) = True

或去select

Sheet1.MyListBox.Selected(0) = False

使用 XLwings,我可以通过以下操作轻松检查某个项目是否 selected:

>> wb.Sheets(1).api.MyListBox.Selected(0)
True

但是如果我尝试分配一个值,我会得到一个错误:

wb.Sheets(1).api.MyListBox.Selected(0) = True
  File "<ipython-input-156-9ef416a5660f>", line 1
    wb.Sheets(1).api.MyListBox.Selected(0) = True
                                                 ^
SyntaxError: can't assign to function call

希望有人能提供帮助。

PS:我发现如果您处于设计模式(selected 在 Excel 工具栏上),即使第一行在 Python 上也不起作用出于某种原因!

Comment: What if ... the method was not implemented? ... use ListBox.Selected(0) = True and find it is impossible with xlwings

当前库中没有ListBox.Selected(Int32)。 因此我认为它必须在 xlwings 中实现才能可用。
使用 .api 界面,您只能使用图书馆提供的任何内容。


Question: select items programmatically from a listbox

wb.Sheets(1).api.MyListBox.SetSelected(0, True)

ListBox.SetSelected Method (Int32, Boolean)

Selects or clears the selection for the specified item in a ListBox.
Parameters
index Type: System.Int32
The zero-based index of the item in a ListBox to select or clear the selection for.
value Type: System.Boolean
true to select the specified item; otherwise, false.