Format a list using XML and Python

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.

 

 

Would you like to receive updates by email?

Published by

Shannon Graham

Shannon has been writing brief essays and occasional how-to articles at Rocketships, Unaffiliated (.ca) whenever inspiration strikes, since 2012.

She is interested in your opinion.

2 thoughts on “Format a list using XML and Python”

  1. I’m impressed! I’m only learning the basics of CSS code through the few books I find in the library or information from off the internet. It’s a fun process. It’s a bit like solving a puzzle sometimes.

    I have a few sci-fi book recommendations if you care to hear some!

    Alex

Leave a Reply

Your email address will not be published. Required fields are marked *