带有python/COM的Windows应用程序自动化 [英] Windows Application Automation with python/COM
问题描述
我正在尝试在后台为用户自动执行ChemDraw,最好避免使用SendKeys(),因为我认为这需要ChemDraw实例才能可见.我需要做的是以编程方式单击编辑"->复制为"->"InChI",然后从Windows剪贴板中检索结果.
I'm trying to automate ChemDraw for a user in the background, preferably avoiding using SendKeys() as I believe that requires a ChemDraw instance to be visible for that to work. What I need to do is somehow click Edit -> Copy As -> InChI programmatically, then retrieve the result from the Windows clipboard.
我们当前正在使用Python和COM脚本来尝试这种操作.这是我们当前的代码:
We're currently using Python and COM scripting to attempt this. Here's our current code:
# Opens ChemDraw and loads the file, while keeping the window hidden.
ChemDraw = w32.DispatchEx('ChemDraw.Application') # ChemDraw Application Object
ChemDraw.Visible = False # Makes Invisible
Compound= ChemDraw.Documents.Open(cdx_filepath) # ChemDraw File Object (Can be seen with ChemDraw.Activate())
# Selects the whole molecule.
Compound.Objects.Select()
# Here is where we need to figure out how to do CopyAs and Save off clipboard content.
# Saves the file and Quits afterwards.
Compound.SaveAs(jpg_filepath)
ChemDraw.Quit()
我想我有两个问题:如何访问工具栏中的编辑"及其中的结果值?如何从类似"ChemDraw = w32.DispatchEx('ChemDraw.Application')"的行中生成的对象,并确定您可以使用该对象做什么?问题的一部分是,我们似乎无法内省最终的DispatchEx对象,因此我们很难回答自己的问题.
I guess I have two questions: how do we access "Edit" in the toolbar and the resulting values in it? How do take the resulting object made from a line like "ChemDraw = w32.DispatchEx('ChemDraw.Application')" and determine what you can do with it? Part of the issue is that we can't seem to introspect the resulting DispatchEx object, thus we're having a hard answering our own question.
推荐答案
由于COM脚本的复杂性,我认为您不能真正访问编辑菜单",但是有一种解决方案来访问和存储InChI字符串:
Because of the intricacies of COM scripting, I don't think you can really "access the edit menu", but there's a solution to access and store an InChI string:
对于初学者,我强烈建议您使用win32com上的comtypes,因为当您使用dir()时,它会提供更多信息,据我所知,语法几乎是相同的. Win32com几乎一无所获,因此,您实质上是在暗室中寻找仅用于功能的针(除非您有可用的SDK).在这里,打开ChemDraw文件,访问Objects类,然后使用Data()方法并输入"chemical/x-inchi"(在您的情况下).我也一直在与ChemDraw合作开发一个项目,而且我必须做同样的事情,所以这就是你想要的:
For starters, I highly recommend you use comtypes over win32com because that gives a lot more information when you use dir(), and the syntax is almost identical as far as I can tell. Win32com gives little to nothing, so you're essentially looking for a needle in a dark room for mere functions (unless you have an SDK available). From there, you open the ChemDraw file, access the Objects class, then use the Data() method and input "chemical/x-inchi" (in your case). I've been working with ChemDraw for a project as well, and I had to do the same thing, so here's what you want:
import comtypes.client as w32
# Path for experimental ChemDraw File.
cdx_file = # Insert ChemDraw file path here
# Creates invisible ChemDraw object.
ChemDraw = w32.CreateObject("ChemDraw.Application")
ChemDraw.Visible = True
# Creates file object.
Compound = ChemDraw.Documents.Open(cdx_file)
# Converts file to InChI string.
file_inchi = Compound.Objects.Data("chemical/x-inchi")
print(file_inchi)
# Closes ChemDraw
ChemDraw.Quit()
P.S.:CreateObject是win32com的DispatchEx()的等效类型.
P.S.: CreateObject is the comtypes equivalent of DispatchEx() for win32com.
商品类型文档: https://pythonhosted.org/comtypes/
ChemDraw SDK: http://www.cambridgesoft. com/services/documentation/sdk/chemdraw/ActiveX11/ChemDrawControl10_P.html
ChemDraw SDK: http://www.cambridgesoft.com/services/documentation/sdk/chemdraw/ActiveX11/ChemDrawControl10_P.html
这篇关于带有python/COM的Windows应用程序自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!