Friday, 23 November 2012

Convert-PptxToPDF.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This function converts a PPTx file into a PDF file 
  4. .DESCRIPTION 
  5.     The Convert-PptxToPDF function first creates an  
  6.     instance of PowerPoint, opens the $ifile and saves 
  7.     this to $ofile as a PDF file. 
  8. .NOTES 
  9.     File Name  : Convert-PptxToPDF 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 3.0, Office 2010 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.      
  16. .EXAMPLE 
  17.     There is nothing to see, except a set of new PDF Files in the output folder         
  18.  
  19. #> 
  20.  
  21. Function Convert-PptxToPDF { 
  22.  
  23. [CmdletBinding()] 
  24. Param( 
  25. $IFile, 
  26. $OFile 
  27. ) 
  28.  
  29. # add key assemblies 
  30. Add-type -AssemblyName office -ErrorAction SilentlyContinue 
  31. Add-Type -AssemblyName microsoft.office.interop.powerpoint -ErrorAction SilentlyContinue 
  32.  
  33. # Open PowerPoint 
  34. $ppt = new-object -com powerpoint.application 
  35. $ppt.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse 
  36.  
  37.  
  38. # Open the $Ifile presentation 
  39. $pres = $ppt.Presentations.Open($ifile) 
  40.  
  41. # Now save it away as PDF 
  42. $opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF 
  43. $pres.SaveAs($ofile,$opt) 
  44.  
  45. # and Tidy-up 
  46. $pres.Close() 
  47. $ppt.Quit() 
  48. $ppt=$null 
  49.  
  50. } 
  51.  
  52.  
  53. # Test it 
  54.  
  55. $ipath = "E:\SkyDrive\PowerShell V3 Geek Week\" 
  56.  
  57. Foreach ($ifile in $(ls $ipath -Filter "*.pptx")) { 
  58.   # Build name of output file 
  59.   $pathname = split-path $ifile 
  60.   $filename = split-path $ifile -leaf  
  61.   $file     = $filename.split(".")[0] 
  62.   $ofile    = $pathname + $file + ".pdf" 
  63.  
  64.   # Convert _this_ file to PDF 
  65.    Convert-PptxToPDF -ifile $ifile -OFile $ofile 
  66. } 
Technorati Tags: ,,,

No comments: