Skip to content

Instantly share code, notes, and snippets.

@ddnomad
Created July 20, 2020 11:31
Show Gist options
  • Save ddnomad/67bb739ab780eb7e8a7b902fe23382a2 to your computer and use it in GitHub Desktop.
Save ddnomad/67bb739ab780eb7e8a7b902fe23382a2 to your computer and use it in GitHub Desktop.
Convert PDF documents to DOCX on MacOS
#!/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 "$@"
@ddnomad
Copy link
Author

ddnomad commented Jul 20, 2020

Things to note:

  1. Resulting documents are usually quite broken but this is better than nothing
  2. A couple of trivial fixes should make this script compatible with Linux

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment