Skip to content

Instantly share code, notes, and snippets.

@micjabbour
Forked from mp4096/ppt2pdf.ps1
Last active January 12, 2021 02:30
Show Gist options
  • Save micjabbour/7b7eb63bea812828b1353de1cb0ce4c9 to your computer and use it in GitHub Desktop.
Save micjabbour/7b7eb63bea812828b1353de1cb0ce4c9 to your computer and use it in GitHub Desktop.
Batch convert PowerPoint files to PDF
################################################################################
# forked from https://gist.github.com/mp4096/1a2279ec7b3dfec659f58e378ddd9aee
# Modified to:
# 1) Create PDF files beside the converted PPT files
# 2) Skip already converted PPT files (if a PDF file with the same name already
# exists)
################################################################################
# Batch convert all .ppt/.pptx files encountered in folder and all its subfolders
# The produced PDF files are stored in the invocation folder
#
# Adapted from http://stackoverflow.com/questions/16534292/basic-powershell-batch-convert-word-docx-to-pdf
# Thanks to MFT, takabanana, ComFreek
#
# If PowerShell exits with an error, check if unsigned scripts are allowed in your system.
# You can allow them by calling PowerShell as an Administrator and typing
# ```
# Set-ExecutionPolicy Unrestricted
# ```
# Get invocation path
$curr_path = Split-Path -parent $MyInvocation.MyCommand.Path
# Create a PowerPoint object
$ppt_app = New-Object -ComObject PowerPoint.Application
# Get all objects of type .ppt? in $curr_path and its subfolders
Get-ChildItem -Path $curr_path -Recurse -Filter *.ppt? | ForEach-Object {
# Create a name for the PDF document; they are stored in the beside the PPT file!
# If you want them to be stored in the current directory, replace $_.DirectoryName with $curr_path
$pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf"
# skip file if already converted
If(Test-Path $pdf_filename -PathType Leaf) { return }
Write-Host "Processing" $_.FullName "..."
# Open it in PowerPoint
$document = $ppt_app.Presentations.Open($_.FullName)
# Save as PDF -- 17 is the literal value of `wdFormatPDF`
$opt= [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsPDF
$document.SaveAs($pdf_filename, $opt)
# Close PowerPoint file
$document.Close()
}
# Exit and release the PowerPoint object
$ppt_app.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ppt_app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment