-
-
Save ap0llo/5c5f5aadb885fe918000b248e5dd6e36 to your computer and use it in GitHub Desktop.
# Powershell script to export Powerpoint slides as jpg images using the Powerpoint COM API | |
function Export-Slide($inputFile, $slideNumber, $outputFile) | |
{ | |
# Load Powerpoint Interop Assembly | |
[Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Powerpoint") > $null | |
[Reflection.Assembly]::LoadWithPartialname("Office") > $null | |
$msoFalse = [Microsoft.Office.Core.MsoTristate]::msoFalse | |
$msoTrue = [Microsoft.Office.Core.MsoTristate]::msoTrue | |
# start Powerpoint | |
$application = New-Object "Microsoft.Office.Interop.Powerpoint.ApplicationClass" | |
# Make sure inputFile is an absolte path | |
$inputFile = Resolve-Path $inputFile | |
$presentation = $application.Presentations.Open($inputFile, $msoTrue, $msoFalse, $msoFalse) | |
$slide = $presentation.Slides.Item($slideNumber) | |
$slide.Export($outputFile, "JPG") | |
$slide = $null | |
$presentation.Close() | |
$presentation = $null | |
if($application.Windows.Count -eq 0) | |
{ | |
$application.Quit() | |
} | |
$application = $null | |
# Make sure references to COM objects are released, otherwise powerpoint might not close | |
# (calling the methods twice is intentional, see https://msdn.microsoft.com/en-us/library/aa679807(office.11).aspx#officeinteroperabilitych2_part2_gc) | |
[System.GC]::Collect(); | |
[System.GC]::WaitForPendingFinalizers(); | |
[System.GC]::Collect(); | |
[System.GC]::WaitForPendingFinalizers(); | |
} |
@wale-A If you want to export all slides as JPG you can do it by using the Export
method on the $presentation
object. So it would be $presentation.Export($outputFile, "JPG", $width, $height)
.
$width
and $height
are optional parameters that you can use to specify the resolution of the exported image files. If not specified, the default resolution of 96 dpi will be used.
Have you had any success using this with O365? I think they have pulled the interop module.
I haven't used this script since 2017, but I just tried it with the current version of PowerPint.
I could not get the export to work, but the COM Interop API still seems to be in place, so in principle I think this should still work.
I'm using the Microsoft 365 version 2210.
Which version of Powershell are you using?
Loading the Assembly seems to work fine on Windows PowerShell/PowerShell 5 but it doesn't work for me on PowerShell Core/PowerShell 7
i'm not good with powershell, but how do i run this for all the slides in a ppt file