I don’t think it’s a good idea to first extract the blocks from the layout, because you end up with a collection of blocks that has no reference to the layouts and columns they belonged to.
Here Bastian posted how you can create layouts programmatically:
So I think your best bet would be to actually loop through the layouts and blocks, change what needs to be changed and then to save it all back.
I’m just wondering why all this is even necessary and can’t be done when rendering on the frontend?