There are quite a number of tutorial how that can be done, in say, C# (.NET) or even C++ (COM). There are even PowerShell wrapper.
When it comes to Python, there are of course existing library like PyWinAuto. But what if we want to use UIAutomation API just like the rest of the languages. Since COM should be able to get consumed by any languages, and Python, as usual, should be able to do wonderful things, why not give it a try. However, I have yet to find a tutorial on that. At least I haven't found one. Hence, this tutorial (or rather, sample code):