繁体   English   中英

使用comtypes保存PowerPoint演示文稿时使用文件格式常量

[英]Using file format constants when saving PowerPoint presentation with comtypes

在通过comtypes保存Powerpoint演示文稿时,如何访问可用作文件格式的常量?

在下面的示例中, 32用作格式,但我想使用此处列出的常量)或者至少找到一个带有每个常量值的文档列表。

对于Word,此列表还包含每个常量的值。

import comtypes.client

powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
pres = powerpoint.Presentations.Open(input_path)
pres.SaveAs(output_path, 32)

假设您有一份PowerPoint副本,启动它,按ALT + F11打开VBA编辑器,按F2打开对象浏览器,然后搜索SaveAs以获取此列表。 单击任何常量名称可在对话框底部查看常量的值。

保存成员

您可以通过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的答案中的屏幕截图显示,对于PowerPointPpSaveAsFileType枚举。 我相信在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM