python使用win32com的心得
python使用win32com的心得
https://my.oschina.net/zxcholmes/blog/484789
摘要: 学会如何获取操作windows其它com方法,不依赖网上有限的com操作教程。
主要是有个项目可能要用到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:
- 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:
dir(object.__class__)
The name of a folder can be accessed as follows:
object.Name # Where object refers to a folder.
我就是安装的时候点的太快,结果忘记了路径,重新点击安装,记下了路径。







现在操作ppt的方法我们弄明白了,那么操作word,excel也是一样的道理。顺便封装了一个comppt.py的操作,由于刚写python,代码不是很溜:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | __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' ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架