#!/bin/sh
# Image to an editable Word file, free. From brasspage.com/image-to-editable-word/
# Docling reads each image (text, headings, lists, tables) and pandoc writes the .docx next to it.
# Needs: uv tool install docling   and   brew install pandoc
# Use:   sh image2word.sh page.jpg [more.jpg or .png ...]
# An existing .docx is never overwritten. Docling runs on your own computer; nothing is uploaded.
set -e
[ $# -gt 0 ] || { echo "use: sh image2word.sh page.jpg [more images]" >&2; exit 1; }
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
docling convert "$@" --to md --output "$tmp"
for f in "$@"; do
  out="${f%.*}.docx"
  if [ -e "$out" ]; then
    echo "$out exists, skipped"
    continue
  fi
  pandoc "$tmp/$(basename "${f%.*}").md" -o "$out"
  echo "$f -> $out"
done
