commit 9af5b4725b19b9dab4618b7307a1e36ea3f665b8 Author: Simon Vieille Date: Tue Apr 27 11:25:55 2021 +0200 init terminator diff --git a/README.md b/README.md new file mode 100644 index 0000000..de0568d --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +Excel password terminator +========================= + +This script removes passwords in an Excel file. + +Requirements +------------ + +* `zip` and `unzip` +* `grep` +* `sed` + +On a Debian/Ubuntu system, simply run `sudo apt install zip unzip grep sed`. + +Usage +----- + +``` +./bin/terminate -i excel_with_passwords.xlsx -o excel_without_password.xlsx +``` + +You can turn on verbosity using `-v`. Help is provided with `-h`. + +[]!(https://upload.deblan.org/u/2021-04/6087d86b.jpg) diff --git a/bin/terminate b/bin/terminate new file mode 100755 index 0000000..e74e9e9 --- /dev/null +++ b/bin/terminate @@ -0,0 +1,111 @@ +#!/bin/sh + +usage() { + cat << EOF + +USAGE: + $0 [options] + +OPTIONS: + -i Input file (required) + -o Output file (required) + -v Verbose + -h Show this help + +EOF + +} + +error() { + if [ -n "$1" ]; then + printf "%s\\n" "$1" >/dev/stderr + fi + + exit 1 +} + +exit_if_empty() { + if [ -z "$1" ]; then + error "$2" + fi +} + +log() { + if [ "$VERBOSE" -eq 1 ]; then + printf "%s\\n" "$1" + fi +} + +unzipExcel() { + if [ "$VERBOSE" -eq 0 ]; then + verbosity=-qq + fi + + unzip $verbosity -d "$TEMP_DIRECTORY/excel" "$INPUT_FILE" +} + +createExcel() { + current_dir="$(pwd)" + + if [ "$VERBOSE" -eq 0 ]; then + verbosity=-q + fi + + cd "$TEMP_DIRECTORY/excel" + zip $verbosity -r "../result.xslx" ./* + cd "$current_dir" + mv "$TEMP_DIRECTORY/result.xslx" "$OUTPUT_FILE" + + log "$OUTPUT_FILE generated" +} + +createTmpDirectory() { + TEMP_DIRECTORY="$(mktemp -d)" + log "Temp directory created: $TEMP_DIRECTORY" +} + +removePassword() { + sed -i 's#]*>##' "$1" +} + +removePasswords() { + grep -Rn sheetProtection "$TEMP_DIRECTORY" | cut -d: -f1 | while read -r file; do + removePassword "$file" + log "Password removed in: $file" + done +} + +cleanUp() { + rm -fr "$TEMP_DIRECTORY" && log "Temp directory removed: $TEMP_DIRECTORY" +} + +ERROR=0 +VERBOSE=0 + +while getopts "hvi:o:" option; do + case "$option" in + i) INPUT_FILE="$OPTARG";; + o) OUTPUT_FILE="$OPTARG";; + h) usage; exit 0;; + v) VERBOSE=1;; + :) ERROR=1;; + ?) ERROR=1;; + esac +done + +if [ $ERROR -eq 1 ]; then + error "Invalid parameter\\n$(usage)" +fi + +exit_if_empty "$INPUT_FILE" "Input file is missing (-i)" +exit_if_empty "$OUTPUT_FILE" "Output file is missing (-i)" + +if [ ! -f "$INPUT_FILE" ]; then + error "No such file: $INPUT_FILE" +fi + +createTmpDirectory +unzipExcel +removePasswords +createExcel +cleanUp