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
实例加载底层Type Library,并动态将属性查找转换为typelib访问。由于某种不明原因,它没有包含在comtypes
文档中,即使它现在已经添加了近10年。
另一种选择是在生成的类型库中访问生成模块上的属性,如“具有参数的属性(命名属性)”部分所示。这将使您可以访问与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)
PpSaveAsFileType
枚举类型的简称是PowerPoint
。我相信同样的名称也用于ppSaveAsFileType
枚举类型的文档中;请注意文档标题中添加的(PowerPoint)
。from comtypes.gen import PowerPoint; help(PowerPoint)
来查看已定义的名称,或者参考Microsoft的文档。win32com
的代码示例,则win32com.client.constants
属性的任何使用直接转换为comtypes.client.Constant(...)
或comtypes.gen.<module>
属性。
我没有使用Windows设置来测试任何内容,我是通过阅读文档和comtypes
源代码来推导信息的。
comtypes
而不是win32com
)。 - Martijn Pieters