python加载ocx_在Python中,你可以用COM / ActiveX做什么?

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

I'm fairly familiar with Python and it looks like from what I've read, I might be able to open the report, maybe change some parameters, run it, and export it.

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?

解决方案

First you have to install the wonderful pywin32 module.

It provides COM support. You need to run the makepy utility. It is located at C:...\Python26\Lib\site-packages\win32com\client. On Vista, it must be ran with admin rights.

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

The wrapper is a python module generated in the C:...\Python26\Lib\site-packages\win32com\gen_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 :)

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值