Created
July 22, 2025 16:37
-
-
Save sugatoray/f547f2ed064125cfe272bbe5c18f8f51 to your computer and use it in GitHub Desktop.
pptx-merger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# --- Configuration --- | |
$sourcePath = "C:\Path\To\Your\SourcePpts" # Folder containing the PPTs to merge | |
$outputPath = "C:\Path\To\Your\MergedPresentation.pptx" # Full path for the merged output file | |
# Get a list of PowerPoint files to merge (e.g., all .pptx files) | |
$pptFiles = Get-ChildItem -Path $sourcePath -Filter "*.pptx" | Sort-Object Name | |
# --- Check if there are files to merge --- | |
if ($pptFiles.Count -eq 0) { | |
Write-Host "No PowerPoint files found in: $sourcePath" | |
exit | |
} | |
# --- Create PowerPoint Application Object --- | |
$ppt = New-Object -ComObject PowerPoint.Application | |
$ppt.Visible = $True # Set to $False for background processing | |
# --- Create a New Master Presentation --- | |
$masterPresentation = $ppt.Presentations.Add() | |
# --- Loop through each source PowerPoint file --- | |
foreach ($file in $pptFiles) { | |
$filePath = $file.FullName | |
Write-Host "Processing: $filePath" | |
try { | |
# Open the source presentation | |
$sourcePresentation = $ppt.Presentations.Open($filePath, [Microsoft.Office.Core.MsoTriState]::msoFalse, [Microsoft.Office.Core.MsoTriState]::msoFalse, [Microsoft.Office.Core.MsoTriState]::msoFalse) | |
# Get the number of slides in the source presentation | |
$slideCount = $sourcePresentation.Slides.Count | |
# Insert slides from the source presentation into the master presentation | |
# Important: Use the msoPreserveSourceFormatting option | |
$masterPresentation.Slides.InsertFromFile( | |
$filePath, | |
$masterPresentation.Slides.Count, # Insert after the last slide in master | |
1, # Start index of slides to insert from source (1-based) | |
$slideCount, # Number of slides to insert from source | |
[Microsoft.Office.Core.MsoTriState]::msoTrue # msoPreserveSourceFormatting | |
) | |
# Close the source presentation without saving changes | |
$sourcePresentation.Close() | |
} | |
catch { | |
Write-Error "Error processing $filePath: $($_.Exception.Message)" | |
# Optionally, close the master presentation and exit if a critical error occurs | |
# $masterPresentation.Close() | |
# $ppt.Quit() | |
# exit | |
} | |
} | |
# --- Save the Merged Presentation --- | |
try { | |
$masterPresentation.SaveAs($outputPath) | |
Write-Host "Merged presentation saved to: $outputPath" | |
} | |
catch { | |
Write-Error "Error saving merged presentation: $($_.Exception.Message)" | |
} | |
# --- Clean Up --- | |
$masterPresentation.Close() | |
$ppt.Quit() | |
# Release COM objects to prevent memory leaks | |
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($masterPresentation) | Out-Null | |
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($ppt) | Out-Null | |
Remove-Variable ppt, masterPresentation -ErrorAction SilentlyContinue | |
Write-Host "Script finished." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment