This sort of thing has been done, see “gctrl” which is a Processing script:
Processing is pretty approachable (still really bummed that when I tried to use it to make PDFs they had rounding errors so that dimensions weren’t consistent)
Other candidates in this space are:
- bCNC — written in Python, it actually has a templating system for G-Code
- grblgru — this is a full-fledged design tool