-
-
Save geejay101/ac8dc34a18147dc9679b6bf95f602bf9 to your computer and use it in GitHub Desktop.
Bash Script to Unprotect Sheets in Excel Workbook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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