你可以用 Python 中的 COM/ActiveX 做什么?

2022-01-13 00:00:00 python com pywin32 activex crystal-reports

问题描述

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

I've read that it is possible to automate monthly reports in Crystal Reports with COM/ActiveX. I'm not that advanced to understand what this is or what you can even do with it.

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

I also do a lot of work with Excel and it looks like you also use COM/ActiveX to interface with it.

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

Can someone explain how this works and maybe provide a brief example?


解决方案

首先你要安装精彩的pywin32 模块.

First you have to install the wonderful pywin32 module.

它提供 COM 支持.您需要运行 makepy 实用程序.它位于 C:...Python26Libsite-packageswin32comclient.在 Vista 上,它必须以管理员权限运行.

It provides COM support. You need to run the makepy utility. It is located at C:...Python26Libsite-packageswin32comclient. On Vista, it must be ran with admin rights.

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

This utility will show all available COM objects. You can find yours and it will generate a python wrapper for this object.

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

The wrapper is a python module generated in the C:...Python26Libsite-packageswin32comgen_py folder. The module contains the interface of the COM objects. The name of the file is the COM unique id. If you have many files, it is sometimes difficult to find the right one.

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

After that you just have to call the right interface. It is magical :)

一个简单的excel例子

A short example with 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()

相关文章