我正试图在后台为用户自动化ChemDraw,最好避免使用SendKeys(),因为我认为它需要一个可见的ChemDraw实例才能工作。我需要做的是,以编程方式单击Edit -> Copy As -> InChI,然后从Windows剪贴板中检索结果。

我们目前正在使用Python和COM脚本来尝试这一点。下面是我们当前的代码:
# 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对象,因此我们很难回答我们自己的问题。
发布于 2018-07-25 01:06:43
关于如何访问“编辑”菜单内容的第一个问题是针对ChemDraw本身的,由于没有这个问题,我无法立即给出解决方案。
然而,第二个问题的答案可能会让您自己回答第一个问题,因此,这里是这样的:假设Python对象允许这样做,您可以使用win32com.client.gencache.EnsureDispatch代替DispatchEx,以便为对象自动生成Python类;这允许您更详细地检查对象。除了使用EnsureDispatch,您还可以直接访问基础代码生成功能,这可能更适用于您的工作流。有关详细信息,请参阅this question和this guide。
发布于 2018-07-27 00:13:52
由于COM脚本的复杂性,我不认为您可以真正“访问编辑菜单”,但有一个访问和存储InChI字符串的解决方案:
对于初学者,我强烈建议您使用comtype而不是win32com,因为当您使用dir()时,它提供了更多的信息,而且据我所知,它们的语法几乎相同。Win32com几乎没有提供任何功能,因此您实际上是在暗室中寻找仅用于功能的指针(除非您有可用的SDK )。在这里,打开ChemDraw文件,访问Objects类,然后使用Data()方法并输入"chemical/x-inchi“(在本例中)。我也一直在与ChemDraw合作一个项目,我也必须做同样的事情,所以这是你想要的:
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()附注: CreateObject是win32com的DispatchEx()的comtypes等价物。
Comtype文档:https://pythonhosted.org/comtypes/
ChemDraw SDK:http://www.cambridgesoft.com/services/documentation/sdk/chemdraw/ActiveX11/ChemDrawControl10_P.html
https://stackoverflow.com/questions/51502558
复制相似问题