将ppt转换为PDF时出错

时间:2015-12-29 10:25:13

标签: powershell pdf powerpoint

有些人可以指导我代码出了什么问题吗?我需要将PowerPoint文件转换为PDF文件。

代码:

#Convert Powerpoint formats to pdf
Param(
  [string]$inputPath,
  [string]$outputPath
)
Add-Type -AssemblyName Office
Add-Type -AssemblyName Microsoft.Office.Interop.PowerPoint
$ppFormatPDF = 32

$ppQualityStandard = 0
$pp = New-Object -ComObject PowerPoint.Application
# TODO: Why this property does not work
#$pp.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse

$ppt = $pp.Presentations.Open($inputPath)
$ppt.SaveAs($outputPath, $ppFormatPDF) # 32 is for PDF
$ppt.Close()
$pp.Quit()
$pp = $null 
[gc]::Collect()
[gc]::WaitForPendingFinalizers()

错误:

Exception calling "SaveAs" with "2" argument(s): "Presentation.SaveAs :
PowerPoint can't save ^0 to ^1."
At D:\AllAquent\Rambo\Digo\war\WEB-INF\classes\resources\pptToPdf.ps1:17 char:12
+ $ppt.SaveAs <<<< ($outputPath, $opt) # 32 is for PDF
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ComMethodTargetInvocation

1 个答案:

答案 0 :(得分:0)

几个月前使用this script,在发布此处之前删除了我自己的修改。希望这可能对你有所帮助!

请注意,此脚本旨在将指定目录中的所有Powerpoint演示文稿转换为PDF格式。

Function Convert-PptxToPDF { 

[CmdletBinding()] 
Param( 
$File, 
$OutputFile 
) 

# add key assemblies 
Add-type -AssemblyName office -ErrorAction SilentlyContinue 
Add-Type -AssemblyName microsoft.office.interop.powerpoint -ErrorAction SilentlyContinue 

# Open PowerPoint 
$ppt = new-object -com powerpoint.application 
$ppt.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse 


# Open the $File presentation 
$pres = $ppt.Presentations.Open($file) 

# Now save it away as PDF 
$opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF 
$pres.SaveAs($OutputFile,$opt) 

# and Tidy-up 
$pres.Close() 
$ppt.Quit() 
$ppt=$null 

}

#Where your PDF will be saved
$OutputFile = "C:\Temp\" 

# File-extension could be changed to .pptx if needed
Foreach ($File in $(ls $OutputFile -Filter "*.ppt")) { 
  # Build name of output file 
  $pathname = split-path $File 
  $filename = split-path $File -leaf  
  $rmfileext     = $filename.split(".")[0] 
  $OutputFile    = $pathname + $rmfileext + ".pdf" 

  # Convert _this_ file to PDF 
   Convert-PptxToPDF -file $File -OutputFile $OutputFile 
}  

我不赞成这个剧本。