docx_preview.sh 836 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. usage() {
  4. echo "Usage: $(basename "$0") <file.docx>"
  5. echo "Preview DOCX content as plain text."
  6. exit 1
  7. }
  8. if [ $# -lt 1 ]; then
  9. usage
  10. fi
  11. INPUT="$1"
  12. if [ ! -f "$INPUT" ]; then
  13. echo "Error: File not found: $INPUT"
  14. exit 1
  15. fi
  16. FILE_SIZE=$(du -h "$INPUT" | cut -f1)
  17. echo "=== DOCX Preview: $(basename "$INPUT") ==="
  18. echo "File size: $FILE_SIZE"
  19. if command -v pandoc &>/dev/null; then
  20. CONTENT=$(pandoc -f docx -t plain "$INPUT" 2>/dev/null)
  21. WORD_COUNT=$(echo "$CONTENT" | wc -w | tr -d ' ')
  22. EST_PAGES=$(( (WORD_COUNT + 249) / 250 ))
  23. echo "Word count: $WORD_COUNT"
  24. echo "Estimated pages: $EST_PAGES"
  25. echo "---"
  26. echo "$CONTENT"
  27. else
  28. echo "(pandoc not available, falling back to raw XML extract)"
  29. echo "---"
  30. unzip -p "$INPUT" word/document.xml 2>/dev/null | head -100
  31. fi