Advertisement
s243a

s243a-convert

Nov 10th, 2020 (edited)
1,422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.85 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function soffice_conv(){
  4.   mkdir -p ./tmp_s243a_convert_$$
  5.   soffice --headless --convert-to txt:Text --outdir ./tmp_$$ "$1" #2>/dev/null >/dev/null
  6.   ls -1 ./tmp_$$ | xargs -I % cat '%'
  7.   rm -rf ./tmp_s243a_convert_$$
  8. }
  9. function wvText_conv(){
  10.   mkdir -p ./tmp_s243a_convert_$$
  11.   wvText ./tmp_$$ "$1" ./tmp_$$/"$1".txt #2>/dev/null >/dev/null
  12.   ls -1 ./tmp_$$ | xargs -I % cat '%'
  13.   rm -rf ./tmp_s243a_convert_$$
  14. }
  15. if [ ! -z "`which unoconv`" ]; then
  16.   CONV_PGM=unoconv
  17.   CONV_CMD="unoconv --stdout -f text"
  18. elif [ ! -z "`which soffice`" ]; then
  19.   CONV_PGM=soffice
  20.   CONV_CMD="soffice_conv" #http://hitekhedhelp.blogspot.com/2011/08/omega-overview.html
  21. elif [ ! -z "`which abiword`" ]; then
  22.   CONV_PGM=abiword
  23.   CONV_CMD="abiword --to=txt --to-name=fd://1"
  24. fi
  25. if [ ! -z "`which file`" ]; then #maybe also check that the file command actually works
  26.   if [[ "$(file --mime-type "$1")" = */rtf ]]; then #text/rtf
  27.     if [ ! -z "`which unrtf`" ]; then
  28.       CONV_PGM="unrtf"
  29.       CONV_CMD="unrtf --text"
  30.     fi
  31.     #Possible utilities;
  32.     #TEXTUTIL/ https://superuser.com/questions/243084/rtf-to-txt-on-unix
  33.   elif [[ "$(file --mime-type "$1")" = */msword ]]; then #application/msword
  34.     if [ ! -z "`which antiword`" ]; then
  35.       CONV_PGM="antiword"
  36.       CONV_CMD="antiword"    
  37.     elif [ ! -z "`which wvText`" ] && [ -z "$CONV_CMD" ]; then #Some office tools may be better than this util.
  38.       CONV_PGM="wvText"
  39.       CONV_CMD="wvText_conv"    
  40.     fi
  41.   elif [[  "$(file --mime-type "$1")" = */vnd.oasis.opendocument.* ]]; then #nd.oasis.opendocument
  42.     if [ ! -z "`which odt2txt`" ]; then
  43.       CONV_PGM="odt2txt"
  44.       CONV_CMD="odt2txt"      
  45.     elif [ ! z- "`which odf2txt`" ] && [ -z "$CONV_PGM" ] && [ ! z- "`which python`" ]; then
  46.       CONV_PGM="odf2txt"
  47.       CONV_CMD="odf2txt"    
  48.     fi
  49.   fi
  50. fi
  51. $CONV_CMD "$1"
  52.  
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement