使用 xlwings 操作用户表单
Manipulating userforms using xlwings
我试图找出 Excel 中用户窗体 ComboBox 的 pywin32 对象的方法,但老实说,我不知道自己在做什么,而且一无所获。
VBA 代码(将组合框对象发送到 python):
Private Sub ComboBox1_Change()
s = test(ComboBox1)
End Sub
Python代码:
@xw.func
def test(obj):
print(obj._dict__)
所以,上面的打印返回了这个:
{'_oleobj_': <PyIDispatch at 0x03957A90 with obj at 0x01218C8C>, '_username_': 'IMdcCombo', '_olerepr_': <win32com.client.build.LazyDispatchItem object at 0x03FB0FD0>, '_mapCachedItems_': {}, '_builtMethods_': {}, '_enum_': None, '_unicode_to_string_': None, '_lazydata_': (<PyITypeInfo at 0x03957B50 with obj at 0x0121919C>, <PyITypeComp at 0x03957B68 with obj at 0x012196F4>)}
我想我期待在 VBA 中看到相同的 methods/properties,但我不知道从中得到什么。
有人知道使用 xlwings 直接从 python 操作 userform/controls 的方法吗?
具体来说,我正在寻找向用户窗体动态添加新控件,reading/modifying 控件属性,并理想地修改它们的事件,所有这些都通过 python。
在 documentation 下查看 Shape
下的 xlWings 时,似乎可以访问所有属性。
在 missing features 下,您可以找到使用 .api
访问所有 vba 方法的解决方法。通过它,您可以创建和修改控件,就像您在 VBA 中所做的那样。
您还可以使用 macro(name)
函数在 VBA 中创建函数来修改、创建 comboboxes
并将值传递给函数,即创建一个combobox
在位置 x, y , width, height 并通过 python.
传递这些参数
看起来,您无法通过 xlWings
访问事件。但我发现 IronPython, it uses the .NET interop facilities to access the excel object and events. As you can see under the documentation,您可以像在 C#, VB.NET
等中那样使用 excel 对象。
因此,作为结论,我建议您查看两者的文档。由于它们都向 python 显示 excel 对象,因此您应该能够使用其中之一来执行您想要的操作。
I guess I was expecting to see the same methods/properties found in VBA, but I have no idea what to take from this.
你可以从 this 拿走任何东西,但是 this 不是真正的 Combobox
也不是 [=13] 的东西=] 环境 - 它只是 COM
对象之上的 "wrapper" 对象,通过 IDispatch
接口实现,这可能要归功于 win32com
依赖关系。
因此没有类似 "intellisense" 的功能,但您仍然可以使用 properties/methods:
@xw.func
def test(obj):
# accesing method
obj.AddItem('Hello world!')
# accesing property
obj.Enabled = False
您也可以将 UserForm
作为 obj
传递给 add 一个新控件:
@xw.func
def test(obj):
# add new label
control = obj.Add('Forms.Label.1')
# accesing property
control.Caption = 'Hello world!'
我试图找出 Excel 中用户窗体 ComboBox 的 pywin32 对象的方法,但老实说,我不知道自己在做什么,而且一无所获。
VBA 代码(将组合框对象发送到 python):
Private Sub ComboBox1_Change()
s = test(ComboBox1)
End Sub
Python代码:
@xw.func
def test(obj):
print(obj._dict__)
所以,上面的打印返回了这个:
{'_oleobj_': <PyIDispatch at 0x03957A90 with obj at 0x01218C8C>, '_username_': 'IMdcCombo', '_olerepr_': <win32com.client.build.LazyDispatchItem object at 0x03FB0FD0>, '_mapCachedItems_': {}, '_builtMethods_': {}, '_enum_': None, '_unicode_to_string_': None, '_lazydata_': (<PyITypeInfo at 0x03957B50 with obj at 0x0121919C>, <PyITypeComp at 0x03957B68 with obj at 0x012196F4>)}
我想我期待在 VBA 中看到相同的 methods/properties,但我不知道从中得到什么。
有人知道使用 xlwings 直接从 python 操作 userform/controls 的方法吗?
具体来说,我正在寻找向用户窗体动态添加新控件,reading/modifying 控件属性,并理想地修改它们的事件,所有这些都通过 python。
在 documentation 下查看 Shape
下的 xlWings 时,似乎可以访问所有属性。
在 missing features 下,您可以找到使用 .api
访问所有 vba 方法的解决方法。通过它,您可以创建和修改控件,就像您在 VBA 中所做的那样。
您还可以使用 macro(name)
函数在 VBA 中创建函数来修改、创建 comboboxes
并将值传递给函数,即创建一个combobox
在位置 x, y , width, height 并通过 python.
看起来,您无法通过 xlWings
访问事件。但我发现 IronPython, it uses the .NET interop facilities to access the excel object and events. As you can see under the documentation,您可以像在 C#, VB.NET
等中那样使用 excel 对象。
因此,作为结论,我建议您查看两者的文档。由于它们都向 python 显示 excel 对象,因此您应该能够使用其中之一来执行您想要的操作。
I guess I was expecting to see the same methods/properties found in VBA, but I have no idea what to take from this.
你可以从 this 拿走任何东西,但是 this 不是真正的 Combobox
也不是 [=13] 的东西=] 环境 - 它只是 COM
对象之上的 "wrapper" 对象,通过 IDispatch
接口实现,这可能要归功于 win32com
依赖关系。
因此没有类似 "intellisense" 的功能,但您仍然可以使用 properties/methods:
@xw.func
def test(obj):
# accesing method
obj.AddItem('Hello world!')
# accesing property
obj.Enabled = False
您也可以将 UserForm
作为 obj
传递给 add 一个新控件:
@xw.func
def test(obj):
# add new label
control = obj.Add('Forms.Label.1')
# accesing property
control.Caption = 'Hello world!'