I'm still not seeing why the styles need to be inline. Even less so, now, since you're building the HTML as well, so you know what classes/ids the various elements will have (and of course the <body> element is going to be the <body> element). The whole point of stylesheets is that the stylesheet can be changed (to provide different backgrounds, fonts, etc.) without changing the HTML. The most compelling expression of this fact would be css Zen Garden.
Is it that you're generating the page every time, instead of once at publication time and storing the resulting files (during development you'd want the changes to be immediate even if it does mean more processing)? In that case inline styles still wouldn't be necessary; dalecosp describes one way an entire stylesheet can be embedded into the head of the document.
Finally: even if you are using inline styles when you build the HTML, it strikes me it would be less work putting the right styles in to begin with instead of wrong ones that subsequently have to be replaced.
However, if you have an HTML document you can load it into a DOM structure and get at the contents of all the various "style" attributes. Of course, you'll need to navigate the DOM to find the attributes you want, and the value of the attribute will be a string that you will still need to parse as a list of CSS declarations.