Skip to content

Instantly share code, notes, and snippets.

@geejay101
Forked from zachselby/unprotect-xlsx.sh
Created October 26, 2024 09:48
Show Gist options
  • Save geejay101/ac8dc34a18147dc9679b6bf95f602bf9 to your computer and use it in GitHub Desktop.
Save geejay101/ac8dc34a18147dc9679b6bf95f602bf9 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