Javi

Geekslides: from pptx to pngs

Apr 21st, 2026 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. # ============================================================
  2. # Exporta diapositivas de PowerPoint a PNG + genera README.md
  3. # con las notas del orador convertidas a Markdown
  4. # ============================================================
  5.  
  6. $ErrorActionPreference = "Stop"
  7.  
  8. # Define la carpeta donde buscar los archivos PowerPoint
  9. $source_folder = "."
  10.  
  11. # DPI deseado para la exportación
  12. $dpi = 300
  13.  
  14. # Buscar archivos .ppt / .pptx (evitando el problema de -Include con -Recurse)
  15. $files = Get-ChildItem -Path $source_folder -Recurse -File |
  16. Where-Object { $_.Extension -in ".ppt", ".pptx" }
  17.  
  18. if (-not $files -or $files.Count -eq 0) {
  19. Write-Warning "No se encontraron archivos .ppt/.pptx en '$((Resolve-Path $source_folder).Path)'. Nada que procesar."
  20. return
  21. }
  22.  
  23. Write-Host "Se encontraron $($files.Count) archivo(s) para procesar."
  24.  
  25. # --- Abrir PowerPoint ----------------------------------------
  26.  
  27. try {
  28. $ppt_app = New-Object -ComObject PowerPoint.Application
  29. } catch {
  30. Write-Error "No se pudo iniciar PowerPoint. ¿Está instalado? Detalle: $($_.Exception.Message)"
  31. return
  32. }
  33.  
  34. $processedCount = 0
  35. $errorCount = 0
  36.  
  37. foreach ($file in $files) {
  38. Write-Host "`nProcesando: $($file.FullName)"
  39.  
  40. $presentation = $null
  41.  
  42. try {
  43. $presentation = $ppt_app.Presentations.Open($file.FullName, $true, $false, $false)
  44.  
  45. $folder = Join-Path -Path $file.DirectoryName -ChildPath $file.BaseName
  46. New-Item -ItemType Directory -Force -Path $folder | Out-Null
  47.  
  48. $mdContent = ""
  49. $slideCount = $presentation.Slides.Count
  50.  
  51. if ($slideCount -eq 0) {
  52. Write-Warning " '$($file.Name)' no tiene diapositivas. Se omite."
  53. }
  54.  
  55. for ($i = 1; $i -le $slideCount; $i++) {
  56. Write-Host " Slide $i / $slideCount"
  57.  
  58. try {
  59. $slide = $presentation.Slides.Item($i)
  60.  
  61. $png_filename_relative = "slide_$i.png"
  62. $png_filename = Join-Path -Path $folder -ChildPath $png_filename_relative
  63.  
  64. $mdContent += "### Slide $i`:`n"
  65. $mdContent += "![Slide $i]($png_filename_relative)`n"
  66.  
  67. # Calcular dimensiones según el DPI
  68. $width = [int]($presentation.PageSetup.SlideWidth * $dpi / 72)
  69. $height = [int]($presentation.PageSetup.SlideHeight * $dpi / 72)
  70.  
  71. $slide.Export($png_filename, "PNG", $width, $height)
  72.  
  73. # Extraer notas del orador (placeholder tipo 2 = Body/Notes)
  74. $notes = $slide.NotesPage.Shapes |
  75. Where-Object { $_.PlaceholderFormat.Type -eq 2 } |
  76. ForEach-Object { $_.TextFrame.TextRange.Text }
  77.  
  78. if ($notes) {
  79. $md_filename = Join-Path -Path $folder -ChildPath "slide_$i.md"
  80. $notes | Out-File -FilePath $md_filename -Encoding UTF8
  81.  
  82. $mdNotes = Get-Content -Path $md_filename -Raw
  83. $mdContent += "::: Notes`n`n" + $mdNotes + ":::`n`n"
  84. }
  85. } catch {
  86. Write-Warning " Error procesando slide $i de '$($file.Name)': $($_.Exception.Message)"
  87. $errorCount++
  88. }
  89. }
  90.  
  91. # Colapsar múltiples saltos de línea en uno solo
  92. while ($mdContent -match "`n`n`n") {
  93. $mdContent = $mdContent -replace "`n`n`n", "`n`n"
  94. }
  95.  
  96. $readmePath = Join-Path -Path $folder -ChildPath "README.md"
  97. $mdContent | Out-File -FilePath $readmePath -Encoding UTF8
  98. Write-Host " README.md generado en '$readmePath'"
  99.  
  100. $processedCount++
  101. } catch {
  102. Write-Warning "Error procesando '$($file.FullName)': $($_.Exception.Message)"
  103. $errorCount++
  104. } finally {
  105. if ($presentation) {
  106. try { $presentation.Close() } catch { }
  107. [System.Runtime.InteropServices.Marshal]::ReleaseComObject($presentation) | Out-Null
  108. }
  109. }
  110. }
  111.  
  112. # --- Cerrar PowerPoint -----------------------------------------
  113.  
  114. try { $ppt_app.Quit() } catch { }
  115. [System.Runtime.InteropServices.Marshal]::ReleaseComObject($ppt_app) | Out-Null
  116. [System.GC]::Collect()
  117. [System.GC]::WaitForPendingFinalizers()
  118.  
  119. Write-Host "`nHecho. $processedCount archivo(s) procesado(s), $errorCount incidencia(s)."
Advertisement
Add Comment
Please, Sign In to add comment