Skip to content

Instantly share code, notes, and snippets.

@munepi
Created October 10, 2015 08:52
Show Gist options
  • Save munepi/722fd9ed09957c08bfcf to your computer and use it in GitHub Desktop.
Save munepi/722fd9ed09957c08bfcf to your computer and use it in GitHub Desktop.
Converter XLSX to XLSX deleting formulas after values have been calculated
#!/bin/bash
## Filter Options - Apache OpenOffice Wiki
## https://wiki.openoffice.org/wiki/Documentation/DevGuide/Spreadsheets/Filter_Options
## Requirements:
## - LibreOffice v4.4 or higher version
## - Mac OS X
if [ $# -ne 1 ]; then
cat<<EOF
Usage: $(basename $0) [input xlsx]
If you set specified output directory, you can use the variable OUTDIR below:
OUTDIR=/some/where/output_dir $(basename $0) [input xlsx]
EOF
exit 0
fi
LIBREOFFICE=${LIBREOFFICE:-LibreOffice} # e.g. LibreOfficeDev
OUTDIR=${OUTDIR:-$(pwd)} # $(dirname $1)
## initialize LibreOffice
__soffice=/Applications/${LIBREOFFICE}.app/Contents/MacOS/soffice
if [ ! -x $__soffice ]; then
echo E: $__soffice: No such program
exit 1
fi
killall soffice
##
mkdir -p ${OUTDIR}
xbasename=$(basename $(basename $1 .xlsx) .xls)
xcsv=${OUTDIR}/${xbasename}.csv
xxls=${OUTDIR}/${xbasename}.xls
echo converting to temporally CSV file...
# CSV = Text - txt - csv (StarCalc)
$__soffice --headless --convert-to "csv:Text - txt - csv (StarCalc):44,34,76,1,1/5/2/1/3/1/4/1" --outdir ${OUTDIR} $1
echo converting to XLS file...
$__soffice --headless --infilter="Text - txt - csv (StarCalc):44,34,76,1,1/5/2/1/3/1/4/1" --convert-to "xls:MS Excel 97:UTF8" --outdir ${OUTDIR} ${xcsv}
rm -f ${xcsv}
if [ ! -f ${xxls} ]; then
echo E: failed: ${xxls}
exit 1
fi
echo $(basename $0): finished: ${xxls}
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment