Had the exact same task as the OP, unfortunately the code posted above didn’t help, and I don’t know enough about Python to edit it properly for it to work.
However, I did find this excellent addon: [Addon] replace driver targets - #13 by buzzkirill
which solved the problem very handily