There is a Python package called Sumatra that does this kind of thing, that may be useful directly or as inspiration:
https://pythonhosted.org/Sumatra/