使用xlwings处理用户表单 [英] Manipulating userforms using xlwings

查看:68
本文介绍了使用xlwings处理用户表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出Excel中用户窗体ComboBox的pywin32对象的方法,但老实说,我不知道自己在做什么,却一无所获.

I was trying to find out the methods of the pywin32 object of a userform ComboBox in Excel, but I honestly has no idea what I'm doing and got nowhere.

VBA代码(将组合框对象发送到python):

VBA Code (Sending combobox object to python):

Private Sub ComboBox1_Change()
    s = test(ComboBox1)
End Sub

Python代码:

@xw.func
def test(obj):
    print(obj._dict__)

因此,上面的打印返回了此内容:

So, the print above returned this :

{'_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中的方法/属性相同,但是我不知道该怎么做.

I guess I was expecting to see the same methods/properties found in VBA, but I have no idea what to take from this.

有人知道一种使用xlwings直接从python操作用户窗体/控件的方法吗?

Anyone knows a way to manipulate userform/controls directly from python using xlwings?

特别是我正在寻找通过python动态地向用户窗体添加新控件,读取/修改控件属性以及理想地修改其事件的方法.

Specifically I'm looking for dynamically adding new controls to the userform, reading/modifying controls attributes, and ideally modifying their events, all through python.

推荐答案

我想我期望看到VBA中的方法/属性相同,但是我不知道该怎么做.

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 ,也不是 COM 中的任何内容环境-它只是 COM 对象上的包装"对象,通过 IDispatch 接口,这要归功于 win32com 依赖项.

You can take anything from this, but this isn't a real Combobox nor something from COM environment - it's just a "wrapper" object over a COM object, implemented via IDispatch interface, and it's possibily thanks to the win32com dependency.

因为没有类似智能"的功能,但是您仍然可以使用属性/方法:

Because of that there's no an "intellisense"-like feature, but you're still able to use properties/methods:

@xw.func
def test(obj):
    #   accesing method
    obj.AddItem('Hello world!')
    #   accesing property
    obj.Enabled = False

您还可以将 UserForm 作为 obj 传递给

also you can pass an UserForm as obj to add a new control to it:

@xw.func
def test(obj):
    #   add new label
    control = obj.Add('Forms.Label.1')
    #   accesing property
    control.Caption = 'Hello world!'

这篇关于使用xlwings处理用户表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆