最近有个需要要导出很多PPT,PPTx文件为PDF,因为我的手机不支持查看ppt,但是查看PDF很方面,可以进行PDF上的文字搜索等等。但是手动另存为PDF也是一个很累人的活,为了图方便,查阅了相应文档,写出了下面一个简单的powershell script
关于导出类型,导出PDF的值常量为32,其他导出类型见下面MSDN http://msdn.microsoft.com/en-us/library/bb251061(office.12).aspx
param(
[string]$folder=$(throw("Input the path of folder which contains ppt to export to pdf "))
)
function ExportToPDF($path){
$newname=$path -replace "\.[^.]*$",".pdf"
"exporting $path to $newname "
if(-not (Test-Path $newname)){
$b= $a.Presentations.open($path)
$b.SaveAs($newname,32)
$b.close()
}
}
while( -not (Test-Path $folder)){
$folder=Read-Host -Prompt "input folder path:"
}
Add-type -AssemblyName office
Add-Type -AssemblyName microsoft.office.interop.powerpoint
$a= New-Object -ComObject powerpoint.application
$a.Visible=[Microsoft.Office.Core.MsoTriState]::msoTrue
Get-ChildItem -Path $folder -Include "*.ppt", "*.pptx" -Recurse |%{
exporttopdf $_.fullname
}
$a.quit()
$a = $null
$b= $null
Remove-Variable "a" -Force
Remove-Variable "b" -Force
[gc]::collect()
[gc]::WaitForPendingFinalizers()