How to replace a word inside a .DOCX file using Linux command line?

So, you want to replace things in a brand-specific format? At the first look it looks bad, but the new docx format is a bit better for that than the old doc format, because it's actually a ZIP file containing XML files.

So the answer lies in unzipping it, then you'll have to rummage through the files and figure out on which one to call sed and zip it up again.

Check out the file word/document.xml in the ZIP file.


try this script:

FILE=$1    
RETPATH=`pwd`    
rm -rf /var/tmp/docx    
mkdir /var/tmp/docx    
cp $FILE /var/tmp/docx
cd /var/tmp/docx    
mkdir tmp
unzip $FILE -d tmp
cd tmp/word
sed -i "s/${2}/${3}/" document.xml
cd ..
zip -r ../${FILE} *
cp /var/tmp/docx/${FILE} ${RETPATH}
cd $RETPATH
rm -rf /var/tmp/docx 

and call the script as follows:

./repdocx FILE_NAME OLD_STRING NEW_STRING