Python新手在这里。 所以,请原谅以前是否以不同的格式询问过这个问题。
我试图使用win32com模块在Python中复制以下perl片段。 此片段由Qualcomm提供,以便更轻松地自动化其工具。
use Win32::OLE; use Win32::OLE::Variant; $prod_id = "QPSTAtmnServer.Application"; # AppId for the Automation server. eval{ $qpst = Win32::OLE->GetActiveObject($prod_id)}; # Attempt to use a running instance. die "$prod_id not installed" if $@; unless (defined $qpst) { $qpst = Win32::OLE->new($prod_id, sub {$_[0]->Quit;}) or die "Cannot start $prod_id";} # Start a new instance. Call Quit when $qpst set to undef or script exits. if (defined $qpst) { $port = $qpst->GetPort("COM30001"); }我到目前为止的python代码块如下:
import win32com.client import time import os cmd = 'cls' os.system(cmd) cmd = 'start C:\\LAB\\exe\\pskill.exe QPSTConfig' os.system(cmd) cmd = 'start C:\\LAB\\exe\\pskill.exe QPSTServer' os.system(cmd) cmd = 'start C:\\LAB\\exe\\pskill.exe AtmnServer' os.system(cmd) time.sleep(2) _path = os.getcwd() qpst = win32com.client.Dispatch('QPSTAtmnServer.Application') time.sleep(5) if (qpst is None): print('Darn!') else: port = qpst.GetPort('30001') print(port)它会引发以下错误:
回溯(最近的呼叫最后):
文件“xxxx.py”,第20行,在模块中
port = qpst.GetPort('30001')TypeError:'NoneType'对象不可调用
在阅读了几篇文章之后,似乎方法(GetPort)毕竟没有注册为方法。
这是正确的分析吗?
如果是,我如何让Python将其解释为方法?
如果没有,这里发生了什么错误?
先谢谢您的帮助!
Python newbie here. So, please excuse if this has been asked before in a different format.
I am trying to replicate the following perl snippet in Python using the win32com module. This snippet is provided by Qualcomm for easier automation of their tools.
use Win32::OLE; use Win32::OLE::Variant; $prod_id = "QPSTAtmnServer.Application"; # AppId for the Automation server. eval{ $qpst = Win32::OLE->GetActiveObject($prod_id)}; # Attempt to use a running instance. die "$prod_id not installed" if $@; unless (defined $qpst) { $qpst = Win32::OLE->new($prod_id, sub {$_[0]->Quit;}) or die "Cannot start $prod_id";} # Start a new instance. Call Quit when $qpst set to undef or script exits. if (defined $qpst) { $port = $qpst->GetPort("COM30001"); }The block of python code I have till now is as follows:
import win32com.client import time import os cmd = 'cls' os.system(cmd) cmd = 'start C:\\LAB\\exe\\pskill.exe QPSTConfig' os.system(cmd) cmd = 'start C:\\LAB\\exe\\pskill.exe QPSTServer' os.system(cmd) cmd = 'start C:\\LAB\\exe\\pskill.exe AtmnServer' os.system(cmd) time.sleep(2) _path = os.getcwd() qpst = win32com.client.Dispatch('QPSTAtmnServer.Application') time.sleep(5) if (qpst is None): print('Darn!') else: port = qpst.GetPort('30001') print(port)and it throws the following error:
Traceback (most recent call last):
File "xxxx.py", line 20, in module
port = qpst.GetPort('30001')TypeError: 'NoneType' object is not callable
After reading a couple of posts it seems like the method (GetPort) is not registering as a method after all.
Is that correct analysis?
If yes, how do I make Python interpret it as a method?
If not, what is going on here with the error?
Thanks in advance for the help!
最满意答案
看起来我必须做几件事才能解决这个问题。
在“AtmnServer”OLE TypeLibrary文件上使用makepy命令在以下位置创建* .py文件:
...\Python27\Lib\site-packages\win32com\gen_py\添加一行以实际将所需方法解释为方法(而不是属性/属性或其他内容):
qpst._FlagAsMethod("GetPort")在线之前:
port = qpst.GetPort("COM30001")再次感谢您提供帮助!
It looks like I had to do couple of things to solve the issue.
Use the makepy command on the "AtmnServer" OLE TypeLibrary file to create a *.py file in:
...\Python27\Lib\site-packages\win32com\gen_py\Add an extra line to actually interpret the required Method as a method (instead of as a property/attribute or something) :
qpst._FlagAsMethod("GetPort")before the line:
port = qpst.GetPort("COM30001")Thanks again for offering to help!
更多推荐
发布评论