Wednesday, June 09, 2010

Finding lyrics and converting Word files to text

Two fun, and possibly useful, recipes using IronPython have surfaced in blog entries recently.

This first recipe is from Mark Bloodworth, a Microsoft architect with a fondness for Python and IronPython:
I was looking some lyrics up online this week, so I wondered how hard to would be to write a simple application to find lyrics to your favourite song.  Or to your least favourite song.  Or, in fact, to any arbritrary song.  Via programmableweb, I found the API to lyricsfly, which looked easy to use.  Another IronPython console app beckoned.
Keeping it simple, I decided to use optparse to parse the command-line options and urllib to make the http calls.  This way the program can be called with the user_id that lyricsfly requires (head to their website and you can get a temporary weekly key to try this out) along with the artist name and song title.  What I decided not to do at this stage was to process the resulting XML.  Or handle any errors.  Or handle cases where the user_id, artist or title is not supplied.  But, although rudimentary, it works. ...
While SaveAs method in Word has Encoding parameter, it wasn’t quite clear, how would I specify it from pywin32.
So, my next attempt was to use IronPython, since it has native .NET interface with Office. The biggest advantage of this approach was the fact that you can do dir() on all objects and methods in IronPython shell.
After some googling on encodings, and IronPython Word scripting, here is the script I came up with. ...

No comments:

Post a Comment

Note: only a member of this blog may post a comment.