codekingpro/portable-devtools
114k
1#!/usr/bin/env bash2 3# docx2txt, a command-line utility to convert Docx documents to text format.4# Copyright (C) 2008 Sandeep Kumar5#6# This program is free software; you can redistribute it and/or modify7# it under the terms of the GNU General Public License as published by8# the Free Software Foundation; either version 3 of the License, or9# (at your option) any later version.10#11# This program is distributed in the hope that it will be useful,12# but WITHOUT ANY WARRANTY; without even the implied warranty of13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14# GNU General Public License for more details.15#16# You should have received a copy of the GNU General Public License17# along with this program; if not, write to the Free Software18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA19 20#21# A simple .docx to .txt converter22#23# This script is a wrapper around core docx2txt.pl and saves text output for24# (filename or) filename.docx in filename.txt .25#26# Author : Sandeep Kumar (shimple0 -AT- Yahoo .DOT. COM)27#28# ChangeLog :29#30# 10/08/2008 - Initial version (v0.1)31# 15/08/2008 - Invoking docx2txt.pl with docx document instead of xml file,32# so don't need unzip and rm actions now.33# Removed dependency on sed for generating output filename.34# 23/09/2008 - Changed #! line to use /usr/bin/env - good suggestion from35# Rene Maroufi (info>AT<maroufi>DOT<net) to reduce user work36# during installation.37# 15/09/2009 - Added support for directory (holding unzipped content of38# .docx file) argument to keep it's usage in sync with main39# docx2txt.pl script.40# Fixed bug in condition check for input file accessibility.41# 25/11/2009 - Fixed bug in set expression that was resulting in incorrect42# handling of file/directory names containing spaces.43#44 45 46MYLOC=`dirname "$0"` # invoked perl script docx2txt.pl is expected here.47 48function usage ()49{50 cat << _USAGE_51 52Usage : $0 <file.docx>53 54 <file.docx> can also specify a directory holding the unzipped55 content of a .docx file.56 57_USAGE_58 59 exit 160}61 62[ $# != 1 ] && usage63 64#65# Remove trailing '/'s if any, when input specifies a directory.66#67shopt -s extglob68set "${1%%+(/)}"69 70if [ -d "$1" ]71then72 if ! [ -r "$1" -a -x "$1" ]73 then74 echo -e "\nCan't access/read input directory <$1>!\n"75 exit 176 fi77elif ! [ -f "$1" -a -r "$1" -a -s "$1" ]78then79 echo -e "\nCheck if <$1> exists, is readable and has non-zero size!\n"80 exit 181fi82 83 84TEXTFILE=${1/%.docx/.txt}85[ "$1" == "$TEXTFILE" ] && TEXTFILE="$1.txt" 86 87 88#89# $1 : filename to check for existence90# $2 : message regarding file91#92function check_for_existence ()93{94 if [ -f "$1" ]95 then96 read -p "overwrite $2 <$1> [y/n] ? " yn97 if [ "$yn" != "y" ]98 then99 echo -e "\nPlease copy <$1> somewhere before running the script.\n"100 echeck=1101 fi102 fi103}104 105echeck=0106check_for_existence "$TEXTFILE" "Output text file"107[ $echeck -ne 0 ] && exit 1108 109#110# Invoke perl script to do the actual text extraction111#112 113"$MYLOC/docx2txt.pl" "$1" "$TEXTFILE"114if [ $? == 0 ]115then116 echo -e "\nText extracted from <$1> is available in <$TEXTFILE>.\n"117else118 echo -e "\nFailed to extract text from <$1>!\n"119fi120 121 