问题描述
昨天我试图将一组 PPT 批量转换为一个朋友的 PDF,我决定看看 PowerShell,因为它已经在我的 HD 上放置了一段时间.
Yesterday I was trying to batch convert a group of PPTs into PDFs for a friend, and I decided to have a look at PowerShell, since it's been sitting on my HD for a while.
这是我想出的代码.
$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('somefile.ppt')
$f.ExportAsFixedFormat('some
ewfile.pdf', 2)
由于蛮力"方法不起作用(类型不匹配"),我尝试使用
Since the "brute force" method didn't work ("type mismatch") I tried to import the enum type with
$pptypepdf= [Microsoft.Office.Interop.PowerPoint.PpFixedFormatType]::PpFixedFormatTypePDF
$f.ExportAsFixedFormat('some
ewfile.pdf', $pptypepdf)
这里奇怪的是,它仍然抛出类型不匹配"的错误......
The strange thing here is that it still throws a "type mismatch" error...
另外,SaveAs 也适用于
Also, SaveAs works fine with
$f.SaveAs('some
ewfile.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 中遇到了同样的问题.尝试按照 Stefan Schukat 的解决方案中所述指定 PrintRange
参数:
I've come across the same problem in Python. Try specifying PrintRange
argument as said in solution by Stefan Schukat:
这是 Powerpoint 中的一个错误.它定义了[在,可选的,defaultvalue(0)] PrintRange* PrintRange" 导致生成python包装器中的PrintRange = 0".因此你会得到调用方法时出错.所以makepy没有问题.解决方法使用 PrintRange=None 调用该方法,因为 None 是一个 vali COM 对象.例如.演示文稿.ExportAsFixedFormat(pptFile+'.pdf',win32com.client.constants.ppFixedFormatTypePDF,win32com.client.constants.ppFixedFormatIntentScreen,PrintRange=None)应该可以.
来源:使用PowerPoint 2007的导出功能时类型不匹配
我一点也不了解 PowerShell,但我想出了一个可行的例子:
I don't know PowerShell at all but have worked out a working example:
$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,和我想的一样.附言哦.这是相同的解决方案
This isn't a full solution, but exporting is done. It somehow exports full presentation, not only slide no. 1, as I thought. P.S. Oh. Here's the same solution
这篇关于PowerPoint 2007 SP2,PowerShell 中的 ExportAsFixedFormat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!