python com对象的峰值属性的设置值

发布于 2025-02-04 23:25:05 字数 1782 浏览 4 评论 0原文

我正在使用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]1

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]
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]3

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉溺在你眼里的海 2025-02-11 23:25:05

如果put函数还有其他参数,似乎Win32Com正在努力设置该属性,这有点令人惊讶。

首先要做的是

app = win32com.client.gencache.EnsureDispatch('NAME_OF_APP')

为com对象创建一个Python包装器(而不仅仅是在对象上调用函数并希望)。这本身可能会清除您的问题。

如果没有,这是一种非常丑陋的工作方式。如您所确定的,类型库的相关部分是:

[id(0x00000018),propput, helpstring("property CurrentDevice")]
HRESULT CurrentDevice([in] long lAcq, [in] VARIANT pVal);

您可以使用它以低级别设置属性。

win32com调度对象是pyidispatch对象的包装器。所有调度对象都支持Indoke方法,您可以使用它自己调用该功能。 NB。由于我无法访问您的COM对象,因此无法测试,因此此答案可能需要进行一些调整(!)。

文档

import win32com.client as wc
import pythoncom

app = wc.gencache.EnsureDispatch('NAME OF APP')
app.Visibility=TRUE

newVal = wc.VARIANT(pythoncom.VT_VARIANT,'NAME OF DEVICE')

app._oleobj_.Invoke(24,0,pythoncom.INVOKE_PROPERTYPUT,0,0,newVal)

pyidispatch 但基本上是:

  • 24 = 0x00000018在十进制中:这是属性0 = lCID的ID
  • ,LCID,LOCALE ID ...我总是将其设置为0
  • Pythoncom.invoke_propertyput =呼叫类型。
  • 0 =您是否关心返回类型(您可能不= false)
  • 0 =第一个参数,lacq,如CurrentDevice(0)
  • newval = second paramter,pval,新设备名称,作为变体

我没有尝试过这,但是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

app = win32com.client.gencache.EnsureDispatch('NAME_OF_APP')

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:

[id(0x00000018),propput, helpstring("property CurrentDevice")]
HRESULT CurrentDevice([in] long lAcq, [in] VARIANT pVal);

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 the Invoke 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:

import win32com.client as wc
import pythoncom

app = wc.gencache.EnsureDispatch('NAME OF APP')
app.Visibility=TRUE

newVal = wc.VARIANT(pythoncom.VT_VARIANT,'NAME OF DEVICE')

app._oleobj_.Invoke(24,0,pythoncom.INVOKE_PROPERTYPUT,0,0,newVal)

There are a lot of 'magic' numbers here, but basically:

  • 24 = 0x00000018 in decimal: this is the Id of the property
  • 0 = the LCID, the Locale Id ... I always set it to 0
  • pythoncom.INVOKE_PROPERTYPUT = the type of call.
  • 0 = whether you care about the return type (you probably don't = False)
  • 0 = first parameter, lAcq, as in CurrentDevice(0)
  • newVal = second paramter,pVal, the new device name as a VARIANT

I haven't tried this, but pythoncom is pretty good about converting VARIANT types, so you might not need the VARIANT creation, and can just use NAME OF DEVICE directly as the parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文