Skip to content

Instantly share code, notes, and snippets.

@zachselby
Created August 21, 2020 17:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zachselby/e8143771eb5cc1071a4746f5dbafd1c7 to your computer and use it in GitHub Desktop.
Save zachselby/e8143771eb5cc1071a4746f5dbafd1c7 to your computer and use it in GitHub Desktop.
Bash Script to Unprotect Sheets in Excel Workbook
#!/bin/bash
#################
# Usage: #
#################
# Create an unprotected copy of XLSX file with suffix "-unprotected.xlsx".
# The following command produces `my-workbook-unprotected.xlsx`
# $ ./unprotect-xlsx.sh my-workbook.xlsx
# This is accomplished by removing from all workbook sheets the `sheetProtection` and `protectedRanges` XML elements.
#################
# Dependencies: #
#################
# install xmlstarlet (macos)
#brew install xmlstarlet
# install xmlstarlet (ubuntu)
#apt-get install -y xmlstarlet
filename=$(basename "$1" .xlsx)
filename_unprotected="$filename-unprotected.xlsx"
mkdir -p "$filename"
cp "$1" "$filename/"
cd "$filename"
unzip -q "$1"
rm "$1"
mkdir -p xl/worksheets/filtered
cd xl/worksheets
for i in $(ls -1 *.xml); do xmlstarlet edit -d "//*[local-name()='sheetProtection']" -d "//*[local-name()='protectedRanges']" ${i} > filtered/${i}; done
mv filtered/*.xml .
rmdir filtered
cd ../..
zip -qr "$filename_unprotected" *
mv "$filename_unprotected" ..
cd ..
rm -rf "$filename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment