Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ============================================================
- # Exporta diapositivas de PowerPoint a PNG + genera README.md
- # con las notas del orador convertidas a Markdown
- # ============================================================
- $ErrorActionPreference = "Stop"
- # Define la carpeta donde buscar los archivos PowerPoint
- $source_folder = "."
- # DPI deseado para la exportación
- $dpi = 300
- # Buscar archivos .ppt / .pptx (evitando el problema de -Include con -Recurse)
- $files = Get-ChildItem -Path $source_folder -Recurse -File |
- Where-Object { $_.Extension -in ".ppt", ".pptx" }
- if (-not $files -or $files.Count -eq 0) {
- Write-Warning "No se encontraron archivos .ppt/.pptx en '$((Resolve-Path $source_folder).Path)'. Nada que procesar."
- return
- }
- Write-Host "Se encontraron $($files.Count) archivo(s) para procesar."
- # --- Abrir PowerPoint ----------------------------------------
- try {
- $ppt_app = New-Object -ComObject PowerPoint.Application
- } catch {
- Write-Error "No se pudo iniciar PowerPoint. ¿Está instalado? Detalle: $($_.Exception.Message)"
- return
- }
- $processedCount = 0
- $errorCount = 0
- foreach ($file in $files) {
- Write-Host "`nProcesando: $($file.FullName)"
- $presentation = $null
- try {
- $presentation = $ppt_app.Presentations.Open($file.FullName, $true, $false, $false)
- $folder = Join-Path -Path $file.DirectoryName -ChildPath $file.BaseName
- New-Item -ItemType Directory -Force -Path $folder | Out-Null
- $mdContent = ""
- $slideCount = $presentation.Slides.Count
- if ($slideCount -eq 0) {
- Write-Warning " '$($file.Name)' no tiene diapositivas. Se omite."
- }
- for ($i = 1; $i -le $slideCount; $i++) {
- Write-Host " Slide $i / $slideCount"
- try {
- $slide = $presentation.Slides.Item($i)
- $png_filename_relative = "slide_$i.png"
- $png_filename = Join-Path -Path $folder -ChildPath $png_filename_relative
- $mdContent += "### Slide $i`:`n"
- $mdContent += "`n"
- # Calcular dimensiones según el DPI
- $width = [int]($presentation.PageSetup.SlideWidth * $dpi / 72)
- $height = [int]($presentation.PageSetup.SlideHeight * $dpi / 72)
- $slide.Export($png_filename, "PNG", $width, $height)
- # Extraer notas del orador (placeholder tipo 2 = Body/Notes)
- $notes = $slide.NotesPage.Shapes |
- Where-Object { $_.PlaceholderFormat.Type -eq 2 } |
- ForEach-Object { $_.TextFrame.TextRange.Text }
- if ($notes) {
- $md_filename = Join-Path -Path $folder -ChildPath "slide_$i.md"
- $notes | Out-File -FilePath $md_filename -Encoding UTF8
- $mdNotes = Get-Content -Path $md_filename -Raw
- $mdContent += "::: Notes`n`n" + $mdNotes + ":::`n`n"
- }
- } catch {
- Write-Warning " Error procesando slide $i de '$($file.Name)': $($_.Exception.Message)"
- $errorCount++
- }
- }
- # Colapsar múltiples saltos de línea en uno solo
- while ($mdContent -match "`n`n`n") {
- $mdContent = $mdContent -replace "`n`n`n", "`n`n"
- }
- $readmePath = Join-Path -Path $folder -ChildPath "README.md"
- $mdContent | Out-File -FilePath $readmePath -Encoding UTF8
- Write-Host " README.md generado en '$readmePath'"
- $processedCount++
- } catch {
- Write-Warning "Error procesando '$($file.FullName)': $($_.Exception.Message)"
- $errorCount++
- } finally {
- if ($presentation) {
- try { $presentation.Close() } catch { }
- [System.Runtime.InteropServices.Marshal]::ReleaseComObject($presentation) | Out-Null
- }
- }
- }
- # --- Cerrar PowerPoint -----------------------------------------
- try { $ppt_app.Quit() } catch { }
- [System.Runtime.InteropServices.Marshal]::ReleaseComObject($ppt_app) | Out-Null
- [System.GC]::Collect()
- [System.GC]::WaitForPendingFinalizers()
- Write-Host "`nHecho. $processedCount archivo(s) procesado(s), $errorCount incidencia(s)."
Advertisement
Add Comment
Please, Sign In to add comment