Skip to content

Instantly share code, notes, and snippets.

@jakeydevs
Created June 26, 2020 10:28
Show Gist options
  • Save jakeydevs/ea3879f634efbc83ca7e7ba95061b342 to your computer and use it in GitHub Desktop.
Save jakeydevs/ea3879f634efbc83ca7e7ba95061b342 to your computer and use it in GitHub Desktop.
Export slides as higher DPI in Microsoft 365 - Powershell
# Load file
$inFile = Read-Host -Prompt '👈 Input File'
# No idea what this does, but it needs it otherwise everything crashes
$officeFalse = [Microsoft.Office.Core.MsoTristate]::msoFalse
$officeTrue = [Microsoft.Office.Core.MsoTristate]::msoTrue
# Load Powerpoint Interop Assembly
[Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Powerpoint") > $null
[Reflection.Assembly]::LoadWithPartialname("Office") > $null
# start Powerpoint and create file
$application = New-Object "Microsoft.Office.Interop.Powerpoint.ApplicationClass"
$presentation = $application.Presentations.Open($inFile, $officeTrue, $officeFalse, $officeFalse)
# Get what we are trying to load
[int] $slideNumber = Read-Host -Prompt '🔢 Slide number (or 0 for all)'
if ($slideNumber -eq 0) {
$outFolder = Read-Host -Prompt '👉 Output Folder'
} else {
$outFolder = Read-Host -Prompt '👉 Output Filename'
$outFolder = $outFolder + ".png"
}
# Image info
$outFileType = 'PNG'
$dpi = Read-Host -Prompt '🖼 DPI Export'
# Set resolution for DPI stuff
$outResWidth = 1280
$outResHeight = 720
if ($dpi -eq 50) {
$outResWidth = 667
$outResHeight = 375
} elseif ($dpi -eq 200) {
$outResWidth = 2667
$outResHeight = 1500
} elseif ($dpi -eq 250) {
$outResWidth = 3333
$outResHeight = 1875
} elseif ($dpi -eq 300) {
$outResWidth = 4000
$outResHeight = 2250
}
# Exporting time
if ($slideNumber -eq 0) {
# Export everything
$presentation.Export($outFolder, $outFileType, $outResWidth, $outResHeight)
} else {
# JUST the slide we want
$slide = $presentation.Slides.Item(1)
$slide.Export($outFolder, $outFileType, $outResWidth, $outResHeight)
}
# Cleanup - v important
$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();
@jakeydevs
Copy link
Author

Seems to only really work in Windows Powershell ISE 🤷‍♂️

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