【问题标题】:What can you do with COM/ActiveX in Python? [closed]你可以用 Python 中的 COM/ActiveX 做什么? [关闭]
【发布时间】:2010-11-07 03:23:32
【问题描述】:

我了解到可以使用 COM/ActiveX 在 Crystal Reports 中自动生成月度报告。我没有那么先进来理解这是什么,或者你甚至可以用它做什么。

我也用 Excel 做了很多工作,看起来你也使用 COM/ActiveX 来与它交互。

有人能解释一下这是如何工作的吗?也许可以提供一个简短的例子?

【问题讨论】:

标签: python com crystal-reports activex pywin32


【解决方案1】:

首先你必须安装美妙的pywin32 模块。

它提供 COM 支持。您需要运行makepy 实用程序。它位于C:\...\Python26\Lib\site-packages\win32com\client。在 Vista 上,它必须以管理员权限运行。

此实用程序将显示所有可用的 COM 对象。你可以找到你的,它会为这个对象生成一个 python 包装器。

包装器是在C:\...\Python26\Lib\site-packages\win32com\gen_py 文件夹中生成的python 模块。该模块包含 COM 对象的接口。文件的名称是 COM 唯一 ID。如果您有很多文件,有时很难找到合适的。

之后,您只需调用正确的接口即可。太神奇了:)

一个简单的excel例子

import win32com.client

xlApp = win32com.client.Dispatch("Excel.Application")
xlApp.Visible=1

workBook = xlApp.Workbooks.Open(r"C:\MyTest.xls")
print str(workBook.ActiveSheet.Cells(i,1))
workBook.ActiveSheet.Cells(1, 1).Value = "hello"                
workBook.Close(SaveChanges=0) 
xlApp.Quit()

【讨论】:

  • 如果您收到关于未注册内容的错误,请确保您使用的是 32 位 Python(如果您使用的是 32 位 com 对象)。 python.6.x6.nabble.com/…
  • "import pythoncom" 不是必需的
  • 我修复了“import pythoncom”不是必需的
【解决方案2】:

您基本上可以执行后期绑定的等效操作。因此,通过 IDispatch 公开的任何内容都可以被使用。

这是我这个周末编写的一些代码,用于通过 Windows Image Acquisition 2.0 从 twain 设备获取图像,并将数据放入我可以推送到基于 gtk 的 UI 中的东西中。

WIA_COM = "WIA.CommonDialog"
WIA_DEVICE_UNSPECIFIED = 0
WIA_INTENT_UNSPECIFIED = 0
WIA_BIAS_MIN_SIZE = 65536
WIA_IMG_FORMAT_PNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"

def acquire_image_wia():
    wia = win32com.client.Dispatch(WIA_COM)
    img = wia.ShowAcquireImage(WIA_DEVICE_UNSPECIFIED,
                           WIA_INTENT_UNSPECIFIED,
                           WIA_BIAS_MIN_SIZE,
                           WIA_IMG_FORMAT_PNG,
                           False,
                           True)
    fname = str(time.time())
    img.SaveFile(fname)
    buff = gtk.gdk.pixbuf_new_from_file(fname)
    os.remove(fname)

return buff

它不漂亮,但它有效。我会断言它等同于您必须在 VB 中编写的内容。

【讨论】:

    【解决方案3】:

    这是一个有效的解决方案,它可以创建文件并为单元格添加价值:

    import win32com.client
    import xlsxwriter
    import os
    cwd = os.getcwd()
    file_path = cwd + "\\test.xlsx"
    
    #Create an excel file
    workbook = xlsxwriter.Workbook(file_path)
    worksheet = workbook.add_worksheet()
    workbook.close()
    
    #Open an excel application
    xlApp = win32com.client.Dispatch("Excel.Application")
    xlApp.Visible=1
    
    
    workBook = xlApp.Workbooks.Open(file_path)
    print str(workBook.ActiveSheet.Cells(1,1))
    workBook.ActiveSheet.Cells(1, 1).Value = "hello55"                
    workBook.Close(SaveChanges=1) 
    xlApp.Quit()
    

    【讨论】:

      【解决方案4】:

      如何在 python 3 中接收 ActiveX 事件

      # coding=utf8
      
      from PyQt5.QAxContainer import *
      from PyQt5.QtWidgets import *
      from PyQt5.QtCore import QObject
      import sys
      
      TITLE = "CallX Python Example: accept any calls"
      TrueConfCallX_Class = '{27EF4BA2-4500-4839-B88A-F2F4744FE56A}'
      
      SERVER = '' # empty - connect to TrueConf Online cloud
      USER = '<trueconf id>'
      PASSWORD = '<password>'
      
      class CallXWindow(QWidget):
      
          def __init__(self):
              QAxWidget.__init__(self)
              self.setWindowTitle(TITLE)
              self.move(400, 30)
      # end of class CallXWindow(QWidget)
      
      
      class ActiveXExtend(QObject):
      
          def __init__(self, view):
              super().__init__()
              self.view = view
              self.ocx = QAxWidget(TrueConfCallX_Class)
      
              self.ocx.move(0, 0)
              self.ocx.setFixedSize(640, 375)
              self.ocx.setParent(self.view)
              self.ocx.show()
      
              # receive some ActiveX events 
              self.ocx.OnXAfterStart.connect(self._OnXAfterStart)
              self.ocx.OnServerConnected[str].connect(self._OnServerConnected)
              self.ocx.OnLogin[str].connect(self._OnLogin)
              self.ocx.OnInviteReceived[str].connect(self._OnInviteReceived)
              self.ocx.OnXError[int, str].connect(self._OnXError)
              self.ocx.OnXLoginError[int].connect(self._OnXLoginError)
      
          # Events
          def _OnXAfterStart(self):
              print("**OnXAfterStart")
              # select devices
              self.ocx.XSetCameraByIndex(0)
              self.ocx.XSelectMicByIndex(0)
              self.ocx.XSelectSpeakerByIndex(0)
              # connect to server
              self.ocx.connectToServer(SERVER)
      
          def _OnServerConnected(self, eventDetails):
              print("**OnServerConnected")
              print(eventDetails)
              # login
              self.ocx.login(USER, PASSWORD)
      
          def _OnLogin(self, eventDetails):
              print("**OnLogin")
      
          def _OnInviteReceived(self, eventDetails):
              print("**OnInviteReceived")
              print(eventDetails)
              # accept any calls
              self.ocx.accept()
      
          def _OnXError(self, errorCode, errorMsg):
              print("**OnXError")
              print('{}. Code: {}'.format(errorMsg, errorCode))
      
          def _OnXLoginError(self, errorCode):
              print("**OnXLoginError")
              if errorCode == 8:
                  print('Support for SDK Applications is not enabled on this server')
              else:
                  print('Login error. Code: {}'.format(errorCode))
      # end of class ActiveXExtend(QObject)
      
      
      if __name__ == '__main__':
          app = QApplication(sys.argv)
          MainWindow = CallXWindow()
          axwin = ActiveXExtend(MainWindow)
          MainWindow.show()
      sys.exit(app.exec_())
      

      【讨论】:

        猜你喜欢
        • 2011-01-05
        • 1970-01-01
        • 2016-09-13
        • 1970-01-01
        • 2021-03-18
        • 1970-01-01
        • 2010-09-26
        • 2010-11-25
        • 2012-11-17
        相关资源
        最近更新 更多