Format a list using XML and Python

by | Jan 7, 2013

I made a nice gallery full of photographs of all the books currently on my bookshelf. In the next few months I’ll be getting rid of all of them, and it will be tough to let go of some. So this is for the memories.

By the way, I finally figured out the lightbox gallery that I was moaning about in a previous post, many months ago. I’m using it to display the pictures all nice and fancy. But there are almost a hundred of them, and that makes for some tedious html and xml coding. Fortunately, with all the education I’ve recently acquired, it was a little easier. I used a PHP script to grab the filenames of the pictures, thumbnail versions, author name and title of the books out of an xml file and spit it out in html.

The tough part was the xml file. The finished product was over a thousand lines long and I’m not typing that crap.

First, put all the thumbnails in one folder with names like booktitle-t.jpg. The large versions go in another folder with names like booktitle.jpg. Go like this…

…from the command line to get lists of all the file names.

Then use this quick python script to add the xml tags.

so that gets you this:

Awesome.  Add lines for the thumbnails, author and title as well.

Which gets you this:

Then just add the Author and Title fields by hand, unless you have a better idea. Run it like so:

I suppose there’s a proper way to write to a text file from within the script, but I never got around to looking that up.