Created
July 20, 2020 11:31
-
-
Save ddnomad/67bb739ab780eb7e8a7b902fe23382a2 to your computer and use it in GitHub Desktop.
Convert PDF documents to DOCX on MacOS
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
#!/usr/bin/env bash | |
set -euo pipefail | |
readonly LIBRE_OFFICE_EXEC=/Applications/LibreOffice.app/Contents/MacOS/soffice | |
function main { | |
if test "$(uname)" != 'Darwin'; then | |
echo "[X] This script is compatible only with MacOS" | |
exit 1 | |
fi | |
if ! test -f "${LIBRE_OFFICE_EXEC}"; then | |
echo "[X] LibreOffice installation is required for this script to work" | |
echo "[X] See: https://www.libreoffice.org/get-help/install-howto/macos/" | |
exit 1 | |
fi | |
for fname in *.pdf; do | |
local outf | |
outf="$(echo "${fname}" | rev | cut -f2- -d '.' | rev).docx" | |
echo "[i] Converting a PDF file: ${fname}" | |
if ! "${LIBRE_OFFICE_EXEC}" \ | |
--invisible \ | |
--infilter='writer_pdf_import' \ | |
--convert-to docx:'MS Word 2007 XML' \ | |
"${fname}" > /dev/null; | |
then | |
echo '---(!!!) Conversion failed' | |
exit 1 | |
fi | |
if ! test -f "${outf}"; then | |
echo '---(!!!) Something is royally fucked' | |
exit 1 | |
fi | |
echo "---(i) Output file name: ${outf}" | |
done | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Things to note: