This will replace the pid in wmctrl -lp’s output with the corresponding command, if one is found:
wmctrl -lp | awk '{ pid=$3; cmd="ps -o comm= " pid; while ((cmd | getline command) > 0) { sub(" " pid " ", " " command " ") }; close(cmd) } 1'
This obviously won’t work for windows displaying remote processes; it also will give strange results for windows corresponding to sandboxed processes in some cases (e.g. Flatpak).
The AWK script reads each line, extracts the pid, and runs ps -o comm= to determine the corresponding command; if one is found, it replaces the corresponding pid string with the command.