If you want an open-source single-script solution, take a look at the textdeskew
script by Fred Weinhaus.
If you want to see what the script does: there are a few before-after figures on the page that I've linked to.
There are quite a few command line options, for fine-tuning.
Find it on Fred's ImageMagick Scripts for more, e.g. texctcleaner, also with ...