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

时间:2022-01-19 21:08:46

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.

我想我将不得不在Crystal Reports中运行月度报告。我已经读过你可以使用COM / ActiveX自动执行此操作,但我不是那么先进,无法理解它是什么或者你甚至可以用它做什么。

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.

我对Python非常熟悉,从我看过的内容来看,我可以打开报告,可能会更改一些参数,运行它并导出它。

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

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

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

有人可以解释这是如何工作的,也许可以提供一个简短的例

4 个解决方案

#1


33  

First you have to install the wonderful pywin32 module.

首先,您必须安装精彩的pywin32模块。

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.

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

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

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

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.

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

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

之后你只需要调用正确的界面。这很神奇:)

A short example with excel

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

#2


6  

You can also find useful tips here : http://timgolden.me.uk/python/win32_how_do_i.html it's easy to adapt to any kind of application.

您还可以在这里找到有用的提示:http://timgolden.me.uk/python/win32_how_do_i.html它很容易适应任何类型的应用程序。

#3


3  

You can basically do the equivalent of late binding. So whatever is exposed through IDispatch is able to be consumed.

你基本上可以做相当于后期绑定。因此,通过IDispatch公开的任何内容都可以被消费。

Here's some code I wrote this weekend to get an image from a twain device via Windows Image Acquisition 2.0 and put the data into something I can shove in a gtk based UI.

这是我本周末写的一些代码,用于通过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

It's not pretty but it works. I would assert it's equivalent to what you would have to write in VB.

它不漂亮,但它的工作原理。我断言它等同于你在VB中编写的内容。

#4


1  

Here is a working solution that creates a file and adds value to a cell:

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

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

#1


33  

First you have to install the wonderful pywin32 module.

首先,您必须安装精彩的pywin32模块。

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.

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

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

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

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.

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

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

之后你只需要调用正确的界面。这很神奇:)

A short example with excel

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

#2


6  

You can also find useful tips here : http://timgolden.me.uk/python/win32_how_do_i.html it's easy to adapt to any kind of application.

您还可以在这里找到有用的提示:http://timgolden.me.uk/python/win32_how_do_i.html它很容易适应任何类型的应用程序。

#3


3  

You can basically do the equivalent of late binding. So whatever is exposed through IDispatch is able to be consumed.

你基本上可以做相当于后期绑定。因此,通过IDispatch公开的任何内容都可以被消费。

Here's some code I wrote this weekend to get an image from a twain device via Windows Image Acquisition 2.0 and put the data into something I can shove in a gtk based UI.

这是我本周末写的一些代码,用于通过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

It's not pretty but it works. I would assert it's equivalent to what you would have to write in VB.

它不漂亮,但它的工作原理。我断言它等同于你在VB中编写的内容。

#4


1  

Here is a working solution that creates a file and adds value to a cell:

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

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