昨天,我试图为朋友将一组 PPT 批量转换为 PDF,我决定看看 PowerShell,因为它在我的硬盘上已经有一段时间了。

这是我想出的代码。

$p = new-object -comobject powerpoint.application

# I actually don't know why I have to set the window to visible, 
# but it doesn't work otherwise, anyway, it's not the real problem I have
$p.visible = 1 

$f = $p.presentations.open('\some\file.ppt')

$f.ExportAsFixedFormat('\some\newfile.pdf', 2) 

2 用于 PDF

由于“暴力”方法不起作用(“类型不匹配”),我尝试使用以下命令导入枚举类型

$pptypepdf= [Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::PpFixedFormatTypePDF
$f.ExportAsFixedFormat('\some\newfile.pdf', $pptypepdf) 

这里奇怪的是它仍然抛出“类型不匹配”错误......

另外,另存为也可以很好地工作

$f.SaveAs('\some\newfile.pdf', 32) # 32 is for PDF

我究竟做错了什么?

更新

相关文档:

这是完整的错误消息

$pptypepdf= [Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::PpFixedFormatTypePDF
$f.ExportAsFixedFormat($filepath, $pptypepdf)

Exception calling "ExportAsFixedFormat" with "2" argument(s): "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"

At line:1 char:23
+ $f.ExportAsFixedFormat <<<< ($filepath, $pptypepdf)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

没有正确的解决方案

其他提示

我在Python中也遇到过同样的问题。尝试指定 PrintRange Stefan Schukat 在解决方案中所说的论点:

这是 Powerpoint 中的一个错误。它定义了“[in, optional, defaultvalue(0)] PrintRange* PrintRange“,导致生成 python 包装器中的“PrintRange=0”。因此,你会得到 调用该方法时出错。所以makepy没有问题。解决方法 使用 PrintRange=None 调用该方法,因为 None 是 vali COM 对象。例如。介绍。导出为固定格式(pptFile+'.pdf', win32com.client.constants.ppFixedFormatTypePDF, win32com.client.constants.ppFixedFormatIntentScreen,PrintRange=None) 应该工作。

来源:使用 PowerPoint 2007 的导出功能时类型不匹配


我根本不了解 PowerShell,但已经制定了一个工作示例:

$p.ActivePresentation.PrintOptions.Ranges.Add(1,1)
$r = $p.ActivePresentation.PrintOptions.Ranges.Item(1)
$document.ExportAsFixedFormat('D:\\ps.pdf', 2, 1, 0, 1, 1, 0, $r)

这不是完整的解决方案,但导出已完成。它以某种方式导出完整的演示文稿,而不仅仅是幻灯片编号。1、和我想的一样。附:哦。这里也是一样的 解决方案

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top