python可使用一个第三方库叫作win32com达到操做com的目的,
我是安装了ActivePython的第三方库,从官网下载了安装包,该第三方库几乎封装了全部python下面的win32相关的操做,例如win32api,win32gui等等,能够说是比较齐全的了,下载地址能够自行百度获取。
主要是有个项目可能要用到ppt转换成视频的功能。
以后在想使用com操做excel还有word,ppt的时候,相信大部分人跟我同样,都是搜索python如何操做ppt,或者win32com操做office之类的搜索语句,
搜索完毕以后,点进去以后,几乎都直接是代码了,我以为这样看得我云里雾里的,若是想作些其它操做都作不到,我当时的想法是想知道这些com的操做,究竟在哪里能够查找的到,由于网上只是有限的几个操做,注入往ppt添加图片,或者doc转成pdf之类的,而实际上的office的com操做时不止这些函数的,那么咱们怎么知道其它的api呢?这样子咱们就能够脱离网上的代码,本身编写com代码了。
一番查找,谷歌以后,在一个网页上终于找到了答案:
Querying Interfaces
Now, how does one investigate the detail of each object? For example, how does one access the name of a folder? Firstly, it helps to know the interface that the object exposes, and this information can be found in several places: python
- The Microsoft API documentation.
- Other useful sources, such as the "Outlook Exchange" documentation.
- The interface file generated by the "COM Makepy utility". To know which file is relevant to the interface in question, either perform a "grep" search for the name of the interface on the win32com/gen_py directory within your Python distribution, or invoke an erroneous method or access a non-existent attribute on an object with that interface in order to see what the name of the interface file is.
- The "OLE/COM Object Viewer" in the "Tools" menu in Microsoft Visual C++ (at least in Visual Studio 6.0).
- Once opened, the "Interfaces" section of the information hierarchy can be explored to reveal some "_Application" entries. For one of these, the information pane will mention "Microsoft Outlook 9.0 Object Library" under "TypeLib", for example.
- Double-clicking on an "_Application" entry will provide an "ITypeInfo Viewer" which contains a "_Methods" section describing the available methods on the application's automation object.
- The "Type Libraries" section of the information hierarchy will list, for example, "Microsoft Outlook 9.0 Object Library", and this can be investigated by double-clicking on that entry.
Hopefully, however, the object that you are accessing is known well enough by PythonWin to permit some kind of attribute or method completion on it. You should only need to resort to the above when more detailed knowledge about a method or attribute is required. You can also try something like this: web
dir(object.__class__)
The name of a folder can be accessed as follows: windows
object.Name # Where object refers to a folder.
这里的第四个方法就是我找到的确认有效的,其它三个若是有兴趣的能够试试,第四个方法那就是ole/com object viewer工具,百度之下,下载了一个这样的工具,听说安装了vs以后是有的,
不过因为我不知道可执行程序的名字,也无从找起,因而从新下载了一个完整的工具,安装以后
默认安装路径是:C:\Program Files (x86)\Resource Kit
我就是安装的时候点的太快,结果忘记了路径,从新点击安装,记下了路径。

api
这个工具名字叫作oleview.exe,打开的时候,提示缺乏了什么dll,不要紧。
由于我如今知道名字了,而后使用everything搜索了工具,在个人visual studio里面一样找到了该工具,这下子能够完美打开了。
软件的界面样子大概是:
记得要在右侧的

Type Libraries里面找到相关的library,这里我须要操做的是powerpoint,也就是ppt

找到以后,双击打开它。
在右侧的就是一个列表,左侧的就是对于的内容,刚刚打开的时候,左侧显示的是完整的PowerPoint的api。
因为这个工具,不可以ctrl+f查找,咱们能够ctrl+a,复制左侧的内容到文本中,使用其余诸如sublime文本编辑器执行查找功能。
下面搜索一下:saveAs(大概就是这个意思,我想找一个api能够另存为ppt为视频的操做)

咱们找到了这个函数,同时结合网上的例子,咱们就知道怎么使用了,传入的第一个参数是FileName,顾名思义就是文件名,第二个是int类型的fileFormat,若是是网上的例子的话,多半只会告诉你一个转换成pdf的代码,可是如今我要的是转成视频。
咱们回到ole viewer,看看有没有fileformat的信息。
果不其然,发现了这样的代码:
在最后,我找到了ppSaveAsWMV,很好,这样子咱们就能够结合网上的例子,修改了。
如今操做ppt的方法咱们弄明白了,那么操做word,excel也是同样的道理。顺便封装了一个comppt.py的操做,因为刚写python,代码不是很溜:
app
__author__ = 'zxc'
import win32com.client
import time
import os
ppSaveAsWMV = 37
# only for windows platform and with the microsoft office 2010 or above,it needs the library win32com
def cover_ppt_to_wmv(ppt_src,wmv_target):
ppt = win32com.client.Dispatch('PowerPoint.Application')
presentation = ppt.Presentations.Open(ppt_src,WithWindow=False)
presentation.CreateVideo(wmv_target,-1,4,720,24,60)
start_time_stamp = time.time()
while True:
time.sleep(4)
try:
os.rename(wmv_target,wmv_target)
print 'success'
break
except Exception, e:
pass
end_time_stamp=time.time()
print end_time_stamp-start_time_stamp
ppt.Quit()
pass
if __name__ == '__main__':
cover_ppt_to_wmv('d:\\python\\demo.ppt','d:\\python\\demo.wmv')