Created
August 21, 2020 17:41
-
-
Save zachselby/e8143771eb5cc1071a4746f5dbafd1c7 to your computer and use it in GitHub Desktop.
Bash Script to Unprotect Sheets in Excel Workbook
This file contains 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