Skip to content

Instantly share code, notes, and snippets.

@ap0llo
Created January 11, 2017 20:25
Show Gist options
  • Save ap0llo/5c5f5aadb885fe918000b248e5dd6e36 to your computer and use it in GitHub Desktop.
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
# 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
Copy link

wale-A commented Oct 9, 2017

i'm not good with powershell, but how do i run this for all the slides in a ppt file

@P3W-451
Copy link

P3W-451 commented Oct 26, 2017

@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.

@CraigChamberlain
Copy link

Have you had any success using this with O365? I think they have pulled the interop module.

@ap0llo
Copy link
Author

ap0llo commented Nov 5, 2022

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.

@CraigChamberlain
Copy link

CraigChamberlain commented Nov 7, 2022 via email

@ap0llo
Copy link
Author

ap0llo commented Nov 7, 2022

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

@CraigChamberlain
Copy link

CraigChamberlain commented Nov 7, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment