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