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:

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.

这里的第四个方法就是我找到的确认有效的,其它三个如果有兴趣的可以试试,第四个方法那就是ole/com object viewer工具,百度之下,下载了一个这样的工具,据说安装了vs之后是有的,

不过由于我不知道可执行程序的名字,也无从找起,于是重新下载了一个完整的工具,安装之后

默认安装路径是:C:\Program Files (x86)\Resource Kit

我就是安装的时候点的太快,结果忘记了路径,重新点击安装,记下了路径。

这个工具名字叫做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的信息。 果不其然,发现了这样的代码:

PpSaveAsFileType

双击它

在最后,我找到了ppSaveAsWMV,很好,这样子我们就可以结合网上的例子,修改了。

现在操作ppt的方法我们弄明白了,那么操作word,excel也是一样的道理。 顺便封装了一个comppt.py的操作,由于刚写python,代码不是很溜:

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

python 根据word生成ppt_python操作word、ppt的详解相关推荐

  1. python处理表格和ppt_Python 操作Word(Excel、PPT等通用)

    使用技巧 import win32com from win32com.client import Dispatch, constants w = win32com.client.Dispatch('W ...

  2. Python基本语法_文件操作_读写函数详解

    目录 目录 软件环境 file文件对象 open文件操作 读文件 read读取所有文件内容 readline获取一行内容 readlines读取所有文件内容 readreadlinereadlines ...

  3. poi实现操作生成word表格和操作word中的图表数据

    poi实现操作生成word表格和操作word中的图表数据 写入word表格 1.1首先要将写入的数据整理好,将其封装在一个类中: 1.2 其次将数据写入word,写成表格 2 在word表格中动态修改 ...

  4. Python零基础速成班-第14讲-Python处理Excel和Word,使用openpyxl和docx包详解,图表入门

    Python零基础速成班-第14讲-Python处理Excel和Word,使用openpyxl和docx包详解,图表入门 学习目标 Python处理Excel(使用openpyxl包).图表入门\ P ...

  5. python生成二维码_python生成二维码的实例详解

    python生成二维码的实例详解 版本相关 操作系统:Mac OS X EI Caption Python版本:2.7 IDE:Sublime Text 3 依赖库 Python生成二维码需要的依赖库 ...

  6. pythonxml库_对python 生成拼接xml报文的示例详解

    最近临时工作要生成xml报名,通过MQ接口发送.简单小程序. 自增长拼成xml报文 Test_001.py # encoding=utf-8 import time orderId = '' s1= ...

  7. python中unicode编码表_Python中的字符串操作和编码Unicode详解

    本文主要给大家介绍了关于 Python中的字符串操作和编码Unicode的一些知识,下面话不多说,需要的朋友们下面来一起学习吧. 字符串类型 str:Unicode字符串.采用''或者r''构造的字符 ...

  8. python数据挖掘课程】二十一.朴素贝叶斯分类器详解及中文文本舆情分析

    #2018-04-06 13:52:30 April Friday the 14 week, the 096 day SZ SSMR python数据挖掘课程]二十一.朴素贝叶斯分类器详解及中文文本舆 ...

  9. php操作ElasticSearch搜索引擎流程详解

    更多python.php教程请到友情连接: 菜鸟教程https://www.piaodoo.com 茂名一技http://www.enechn.com ppt制作教程步骤 http://www.tpy ...

最新文章

  1. mysql isreg_`Innodb` MySQL中如何优雅的删除大表跑路
  2. 针对“永恒之蓝(WannaCry)”攻击紧急处置手册
  3. 趣谈设计模式 | 策略模式(Strategy):你还在使用冗长的if-else吗?
  4. 微信小程序开发错误:LoginError {type: ERR_WX_GET_USER_INFO}解决方法
  5. 曲奇饼案例运营管理_昆仑海岸 农村生活污水处理设施智慧运营管理项目荣获典型案例奖...
  6. Android网络性能监控方案
  7. JDK8新特性(六)之Stream流的forEach()方法
  8. javaweb实训第四天上午——员工管理系统-JavaBeanELJSTLMVC思想
  9. 操作系统—多生产者多消费者问题
  10. SourceTree 密码填写正确但是拉取显示密码错误解决
  11. Excel如何快速根据身份证号码计算周岁?
  12. zktime 协议_Zktime8.0安装使用说明及常见故障分析
  13. 【高等数学】第 5 讲 偏导数
  14. 关联规则:R与SAS的比较
  15. focus和onfocus区别
  16. SQL1 从 Customers 表中检索所有的 ID
  17. 如何查看 Mac ssh key
  18. matlab数学建模-神经网络感知器函数
  19. 北京-绕五环骑行(完美环绕)2017-03-11
  20. 操作系统课程设计——Shell编程(用c编写一个Linux的外壳Shell)

热门文章

  1. Node.js报错:UnhandledPromiseRejectionWarning: Unhandled promise rejection
  2. Python爬虫新手入门教学(十):爬取彼岸4K超清壁纸
  3. 直饮净水器什么牌子好,净水器评测
  4. Signing for ‘xxx‘ requires a development team.
  5. Centos7---查看内存型号
  6. greenplum 历史拉链表
  7. 《王者荣耀游戏服务器架构演进(完整版)》读后感
  8. 个人实名认证的几种方案
  9. SQL高级教程(三十)- - SQL NULL 函数
  10. Python的一个小程序题