python com对象的峰值属性的设置值
我正在使用PYWIN32来自动化具有自动化服务器技术接口(以前为OLE Automation Server)的软件中的某些任务。
该软件配备了一些详细的手册,其中包含VBA,C ++或MATLAB中的代码示例,但没有Python。我已经建立了一个Python库,该库可以完成软件中内置的大多数功能,但是在Python中我无法做些部分。
如果此属性包含在峰值com对象中,我将无法更改属性的值。
我能做的: [可见性属性的文档]
import win32com.client
app = win32com.client.Dispatch('NAME_OF_APP')
app.Visibility = True
作为一个例子,使用此代码,我可以更改软件的可见性参数:如果它在有或没有GUI的情况下运行。
我无法做的事情: [用于获取和设置当前设备的文档]
import win32com.client
app = win32com.client.Dispatch('NAME_OF_APP')
app.CurrentDevice(0) = 'NAME OF DEVICE'
然后我得到以下错误:
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
此错误对我来说很有意义,但我找不到方法当这些软件属性以峰值对象的形式出现时。一旦我必须指定索引,我就不知道如何设置值。
据我了解,在C ++中,我们能够因指针而改变价值,但是我们如何在Python中实现同一件事?是否有可能,还是我必须使用与Python并行的一些C ++代码来运行我的库?我在C ++中什么都不知道,所以如果我可以避免这样做,那就很好。
我尝试过的
当然,我尝试的第一件事是将()更改为[]或{},而从逻辑上则不起作用。
然后,我使用pycharms中的评估功能来查看我的应用程序后面隐藏了什么。我希望找到可以设置的子归因于,但我在对象内看不到任何东西: [在CurrentDevice对象上评估的结果]
最后,我尝试了以下方法:
import win32com.client
app = win32com.client.Dispatch('NAME_OF_APP')
curr_device = app.CurrentDevice(0)
curr_device = 'NAME OF DEVICE'
我想将对象影响到一个变量,然后更改值,但当然,这仅重写了使用“设备名称”的变量curr磁门,但失去了与COM对象的任何链接。
我觉得我的问题与以下未解决的问题相似: 如何在Python中设置com对象的索引属性的值?
I am using pywin32 to automate some tasks in software that has an Automation Server technology interface (formerly OLE Automation Server).
This software comes with a somewhat detailed manual with code examples in VBA, C++ or Matlab but no Python. I have built a Python library that can do most of the functionalities built into the software but there are some parts I cannot do in Python.
I cannot change the value of a property if this property is contained in a iterable COM object.
What I can do:
[Documentation for Visibility property]
import win32com.client
app = win32com.client.Dispatch('NAME_OF_APP')
app.Visibility = True
As an example, with this code, I can change the visibility parameter of the software: if it runs with or without GUI.
What I cannot do:
[Documentation for getting and setting current device]
import win32com.client
app = win32com.client.Dispatch('NAME_OF_APP')
app.CurrentDevice(0) = 'NAME OF DEVICE'
I then get the following error:
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
This error makes sense to me but I cannot find a way to set any of these software properties when they come in the form of an iterable object. As soon as I have to specify an index, I don't know how to set the value.
From what I understand, in C++ we are able to change the value because of pointers but how can we achieve the same thing in Python? Is it possible or do I have to use some C++ code in parallel to my Python to run my library? I don't know anything in C++ so if I could avoid doing that, it would be good.
What I have tried
Of course, the 1st thing I tried was to change () to [] or {} which logically didn't work.
Then I used the Evaluate function in PyCharms to see what was hiding behind my app.CurrentDevice. I was hoping to find sub-attributes that I could then set but I don't see anything inside the object:
[Result of Evaluate on the CurrentDevice object]
Finally, I have tried the following:
import win32com.client
app = win32com.client.Dispatch('NAME_OF_APP')
curr_device = app.CurrentDevice(0)
curr_device = 'NAME OF DEVICE'
I wanted to affect the object to a variable and then change the value but of course, this only rewrites the variable curr-device with 'NAME OF DEVICE' but loses any link to COM Object.
I feel like my questions are similar to the following unanswered question:
How can I set the value of an indexed property of a COM object in Python?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果
put
函数还有其他参数,似乎Win32Com正在努力设置该属性,这有点令人惊讶。首先要做的是
为com对象创建一个Python包装器(而不仅仅是在对象上调用函数并希望)。这本身可能会清除您的问题。
如果没有,这是一种非常丑陋的工作方式。如您所确定的,类型库的相关部分是:
您可以使用它以低级别设置属性。
win32com调度对象是
pyidispatch
对象的包装器。所有调度对象都支持Indoke
方法,您可以使用它自己调用该功能。 NB。由于我无法访问您的COM对象,因此无法测试,因此此答案可能需要进行一些调整(!)。文档
pyidispatch 但基本上是:
我没有尝试过这,但是
pythoncom
在转换变体类型方面非常好,因此您可能不需要variant
创建,并且可以直接使用Device
的名称>作为参数。It looks as if win32com is struggling to set the property if there is an additional argument to the
put
function, which is a little surprising.First thing to do is to use
This creates a Python wrapper for the COM object (rather than just firing function calls at the object and hoping). This may in itself clear up your issue.
If not, here is a quite ugly way of working around. As you have identified, the relevant part of the type library is:
And you can use this to set the property at a low level.
win32com dispatch objects are a wrapper for the
PyIDispatch
object. All dispatch objects support theInvoke
method, and you can use this to call the function yourself. NB. Since I don't have access to your COM object, I can't test, so this answer may need some tweaking (!).The PyIDispatch documentation
Try:
There are a lot of 'magic' numbers here, but basically:
I haven't tried this, but
pythoncom
is pretty good about converting VARIANT types, so you might not need theVARIANT
creation, and can just useNAME OF DEVICE
directly as the parameter.