Skip to content

Instantly share code, notes, and snippets.

@isurfer21
Created November 26, 2023 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isurfer21/b447062fa2fe00dee118b1f92a59f03d to your computer and use it in GitHub Desktop.
Save isurfer21/b447062fa2fe00dee118b1f92a59f03d to your computer and use it in GitHub Desktop.
Libre Office file convertor CLI for macOS
#!/bin/zsh
while getopts ":f:" opt; do
case ${opt} in
f )
file=$OPTARG
;;
\? )
echo "Invalid option: -$OPTARG" 1>&2
exit 1
;;
: )
echo "Invalid option: -$OPTARG requires an argument" 1>&2
exit 1
;;
esac
done
shift $((OPTIND -1))
if [ -z "$file" ]; then
echo "Usage: ${0##*/} [-f file] {pdf|epub|html|text|odp|odt}" >&2
exit 1
fi
soffice=/Applications/LibreOffice.app/Contents/MacOS/soffice
case "$1" in
pdf)
$soffice --headless --invisible --convert-to pdf $file
;;
epub)
$soffice --headless --invisible --convert-to epub $file
;;
html)
$soffice --headless --invisible --convert-to "html:XHTML Writer File:UTF8" $file
;;
txt)
$soffice --headless --invisible --convert-to "txt:Text (encoded):UTF8" $file
;;
odp)
$soffice --headless --invisible --convert-to odp $file
;;
odt)
$soffice --headless --invisible --convert-to odt $file
;;
*)
echo "Invalid option: $1" >&2
echo "Usage: $0 [-f file] {pdf|epub|html|text|odp|odt}" >&2
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment