pptx-screenshot.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/bin/bash
  2. # pptx-screenshot.sh — Screenshot PPTX slides as PNG images
  3. # Requires: LibreOffice (soffice), Swift compiler (Xcode CLI tools), macOS
  4. set -euo pipefail
  5. SKILL_DIR="$(cd "$(dirname "$0")/.." && pwd)"
  6. SWIFT_SRC="$SKILL_DIR/scripts/pdf2png.swift"
  7. BINARY="$SKILL_DIR/scripts/pdf2png"
  8. PAGES="1"
  9. SCALE="2.0"
  10. OUTDIR=""
  11. usage() {
  12. cat >&2 <<EOF
  13. Usage: pptx-screenshot.sh <pptx_file> [options]
  14. Options:
  15. --pages <spec> Page specification: "1", "1,3,5", "2-5", "all" (default: "1")
  16. --outdir <dir> Output directory (default: same as input file)
  17. --scale <factor> Rendering scale factor (default: 2.0)
  18. -h, --help Show this help
  19. Examples:
  20. pptx-screenshot.sh presentation.pptx
  21. pptx-screenshot.sh presentation.pptx --pages "1-3" --outdir /tmp/slides
  22. pptx-screenshot.sh presentation.pptx --pages all --scale 3.0
  23. EOF
  24. exit 1
  25. }
  26. if [ $# -lt 1 ]; then
  27. usage
  28. fi
  29. PPTX_FILE="$1"
  30. shift
  31. while [ $# -gt 0 ]; do
  32. case "$1" in
  33. --pages)
  34. PAGES="$2"
  35. shift 2
  36. ;;
  37. --outdir)
  38. OUTDIR="$2"
  39. shift 2
  40. ;;
  41. --scale)
  42. SCALE="$2"
  43. shift 2
  44. ;;
  45. -h|--help)
  46. usage
  47. ;;
  48. *)
  49. echo "Unknown option: $1" >&2
  50. usage
  51. ;;
  52. esac
  53. done
  54. if [ ! -f "$PPTX_FILE" ]; then
  55. echo "Error: file not found: $PPTX_FILE" >&2
  56. exit 1
  57. fi
  58. if [ -z "$OUTDIR" ]; then
  59. OUTDIR="$(dirname "$PPTX_FILE")"
  60. fi
  61. if ! command -v soffice &>/dev/null; then
  62. echo "Error: LibreOffice (soffice) not found." >&2
  63. echo "Install with: brew install --cask libreoffice" >&2
  64. exit 1
  65. fi
  66. if ! command -v swiftc &>/dev/null; then
  67. echo "Error: Swift compiler not found. Install Xcode Command Line Tools:" >&2
  68. echo " xcode-select --install" >&2
  69. exit 1
  70. fi
  71. if [ ! -f "$BINARY" ] || [ "$SWIFT_SRC" -nt "$BINARY" ]; then
  72. echo "Compiling PDF renderer..." >&2
  73. swiftc "$SWIFT_SRC" -o "$BINARY" -O 2>&1
  74. echo "Compilation done." >&2
  75. fi
  76. TMPDIR=$(mktemp -d)
  77. trap 'rm -rf "$TMPDIR"' EXIT
  78. BASENAME="$(basename "$PPTX_FILE" | sed 's/\.[^.]*$//')"
  79. echo "Converting PPTX to PDF..." >&2
  80. soffice --headless --convert-to pdf --outdir "$TMPDIR" "$PPTX_FILE" >/dev/null 2>&1
  81. PDF_FILE="$TMPDIR/$BASENAME.pdf"
  82. if [ ! -f "$PDF_FILE" ]; then
  83. echo "Error: LibreOffice failed to convert $PPTX_FILE to PDF" >&2
  84. exit 1
  85. fi
  86. mkdir -p "$OUTDIR"
  87. "$BINARY" "$PDF_FILE" "$OUTDIR" --pages "$PAGES" --scale "$SCALE"