php, perl, or python script
You would have to manipulate the DXF files directly.
With QCAD 3, you should be able to achieve this with the included ECMAScript API. ECMAScript files can be run without showing any QCAD application windows. We use that for some command line utilities.
We're planning to release a technology preview of QCAD 3 real soon so developers can start implementing their custom solutions.
Stay tuned here: