[英]Using file format constants when saving PowerPoint presentation with comtypes
您可以通过comtypes.client.Constants()
类访问与您加载的COM对象关联的所有枚举名称; 传递你创建的PowerPoint.Application COM对象:
from comtypes.client import Constants, CreateObject
powerpoint = CreateObject("Powerpoint.Application")
pp_constants = Constants(powerpoint)
pres = powerpoint.Presentations.Open(input_path)
pres.SaveAs(output_path, pp_constants.ppSaveAsPDF)
Constants
实例加载基础类型库并动态转换属性查找到typelib访问。 虽然它是在近10年前添加的 ,但它并没有包含在comtypes
文档中。
另一个选项是访问生成的类型库中生成的模块的属性,如带参数的属性(命名属性)部分所示 。 这将使您可以访问与Powerpoint IDL关联的任何常量,包括自动完成支持IDE(一旦通过第一次访问PowerPoint.Application对象生成)。
如果在正在创建的对象上公开类型信息,则在使用CreateObject()
时会自动生成模块。 对于'Powerpoint.Application'
,情况确实如此,因为您没有明确设置接口。 仅当有类型信息可用时,自动界面选择才有效。
枚举名称将添加到顶层的生成模块中,因此请直接使用:
import comtypes.client
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
# only import the generated namespace after the com object has been created
# at least once. The generated module is cached for future runs.
from comtypes.gen import PowerPoint
pres = powerpoint.Presentations.Open(input_path)
pres.SaveAs(output_path, PowerPoint.ppSaveAsPDF)
可以在VBA对象浏览器中找到类型库的短名称; Steve Rindsberg的答案中的屏幕截图显示,对于PowerPoint
的PpSaveAsFileType
枚举。 我相信在ppSaveAsFileType
枚举的文档中也使用相同的名称; 请注意文档标题中的(PowerPoint)
添加。
您还可以使用类型库的GUID以及版本号,但如果必须手动键入,则不会完全键入键盘。
你可以使用from comtypes.gen import PowerPoint; help(PowerPoint)
from comtypes.gen import PowerPoint; help(PowerPoint)
以查看在需要提醒时已定义的名称,或仅引用Microsoft文档。
两种方法都避免使用幻数; 类型库定义本身为您提供符号名称。
如果您找到使用win32com
任何代码示例,那么任何使用win32com.client.constants
属性都会直接转换为comtypes.client.Constant(...)
或comtypes.gen.<module>
属性。
我没有访问Windows设置来实际测试任何这些,我从阅读文档和comtypes
的源代码推断信息。
以下是Microsoft的列表,其中包含每个常量的值:
https://docs.microsoft.com/en-us/office/vba/api/powerpoint.ppsaveasfiletype
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.