Monday, 25 March 2013

unprotecting xlsm or xlsx files

I had to unprotect an xlsm spreadsheet (because the author is on vacation).

I found out how here:
http://datapigtechnologies.com/blog/index.php/hack-into-a-protected-excel-2007-sheet/

I converted it into a crude bash script for in case I need to do it again:

#!/bin/bash
workdir=`dirname $1`

filename=`readlink -f "$1"`
unprotected_filname=`echo $filename | sed s/.xlsm$//`-unprotected.xlsm
tmpdir='/tmp/unprotect'
unzip "$filename" -d $tmpdir

sheetfiles=`find $tmpdir/xl/worksheets/ -iname "*.xml"`
echo $sheetfiles

echo "before replacing"
grep -c sheetProtection $sheetfiles
sed -i 's/]*\/>//g' $sheetfiles

echo "after replacing"
grep -c sheetProtection $sheetfiles

cd $tmpdir
zip -r "$unprotected_filname" *
cd -
rm -rf $tmpdir

1 comment: