PyQt5/PyQt

How to use QAxWidget in PyQt5 to load chemdraw file .cdx?

caiyingchun opened this issue · 5 comments

I tried with the following code, but I failed. When I try to load a .cdx file, nothing happened but window close?

from PyQt5 import QtCore
from PyQt5.QAxContainer import QAxWidget
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QFileDialog


class Window(QWidget):

    def __init__(self, *args, **kwargs):
        super(Window, self).__init__(*args, **kwargs)
        layout = QVBoxLayout(self)
        self.axWidget = QAxWidget("C:\Program Files (x86)\PerkinElmerInformatics\ChemOffice2016\ChemDraw\ChemDraw.exe", self)
        self.axWidget.setProperty("geometry", QtCore.QRect(70, 60, 251, 201))
        
        layout.addWidget(self.axWidget)
        layout.addWidget(QPushButton('ChemDraw', self, clicked=self.onOpenChemDraw))

    def onOpenChemDraw(self):
        path, _ = QFileDialog.getOpenFileName(
            self, '请选择ChemDraw文件', '', 'ChemDraw(*.cdx)')
        if not path:
            return
        # 不显示窗体
        #self.axWidget.dynamicCall('SetVisible (bool Visible)', 'false')
        #self.axWidget.setProperty('DisplayAlerts', False)
        _translate = QtCore.QCoreApplication.translate
        #self.axWidget.setProperty("control", _translate("Form", "{b8350810-35b3-46f8-aa9b-fc649fff06df}:ChemDraw Professional ActiveX Control"))
        self.axWidget.setControl(path)


if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

1、确认 ChemDraw 支持 com组件对象并注册到系统中(可以通过描述2中的方法寻找)
2、通过 oleview.exe 工具查找对应的com对象id,比如wordpad程序
image
image
3、用上面找到的 ProgId 或者 clsid通过 这样的方式加载

axWidget = QAxWidget()
axWidget..setControl('WordPad.Document.1') # 比如WordPad
open('help.html', 'wb').write(axWidget.generateDocumentation().encode())  # 导出帮助文档
# 然后使用axWidget.setControl(path)加载文件 或者
# axWidget.dynamicCall('函数', path)  ## 函数通过上面的帮助文档找找有没有对应的打开文件的函数

I tried as what you did. I can open Chemdraw by self.axWidget.setControl('ChemDraw.Application'), but it didn't load my .cdx file. I don't why.

I tried as what you did. I can open Chemdraw by self.axWidget.setControl('ChemDraw.Application'), but it didn't load my .cdx file. I don't why.

提供下生成的帮助文档
open('help.html', 'wb').write(axWidget.generateDocumentation().encode()) # 导出帮助文档

may be not supported embed cdx.
only find Chem3D Embed