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. # add key assemblies 
  29. Add-type -AssemblyName office -ErrorAction SilentlyContinue 
  30. Add-Type -AssemblyName microsoft.office.interop.powerpoint -ErrorAction SilentlyContinue 
  31.  
  32. # Open PowerPoint 
  33. $ppt = new-object -com powerpoint.application 
  34. $ppt.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse 
  35.  
  36.  
  37. # Open the $Ifile presentation 
  38. $pres = $ppt.Presentations.Open($ifile
  39.  
  40. # Now save it away as PDF 
  41. $opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF 
  42. $pres.SaveAs($ofile,$opt
  43.  
  44. # and Tidy-up 
  45. $pres.Close() 
  46. $ppt.Quit() 
  47. $ppt=$null 
  48.  
  49.  
  50.  
  51. # Test it 
  52.  
  53. $ipath = "E:\SkyDrive\PowerShell V3 Geek Week\" 
  54.  
  55. Foreach ($ifile in $(ls $ipath -Filter "*.pptx")) { 
  56.   # Build name of output file 
  57.   $pathname = split-path $ifile 
  58.   $filename = split-path $ifile -leaf  
  59.   $file     = $filename.split(".")[0] 
  60.   $ofile    = $pathname + $file + ".pdf" 
  61.  
  62.   # Convert _this_ file to PDF 
  63.    Convert-PptxToPDF -ifile $ifile -OFile $ofile 
Technorati Tags: ,,,