Hi, I'm Daniel Roy Greenfeld, and welcome to my blog. I write about Python, Django, and much more.

Titlecasing Markdown Headers with Python

Saturday, September 12, 2015 (permalink)


Recently I've been writing a lot of Markdown. While not as sophisticated as ReStructuredText, it's simplicity is nice for accelerated writing. The problem is that I like to put section headings in titlecase.

What do I mean by titlecase?

go to the room


Go to the Room

See how verb 'Go' and the noun 'Room' have their first letter capitalized? And the 'small words', specifically 'to' (preposition) and 'the' (definate article) are not? That's how title casing works (at least in English). That's what I like to see in my section headings.

In theory one could just use Python's str.title() method to perform this transformation. However, that method is too global in reach. We would end up with:

Go To The Room  # 'to' and 'the' have titlecase, when they should not.

So how do I programmatically (i.e. quickly) ensure that dozens of files scattered across multiple directories have section headers that are accurately titlecased?

Accurately Titlecasing Programmatically

The solution to this is a handy library called titlecase. Empowered by this tool I wrote the following script that allows me to transforms all my markdown files to have titlecased section headings.

Titlecases all markdown section headers in a directory.
Confirmed to work with Python 2.7, 3.3, and 3.4.


  python titlemd.py a-directory/
  python titlemd.py  # defaults to '.'

import fnmatch
import os
import sys

  from titlecase import titlecase
except ImportError:
  print("Please install titlecase")

def main(location):
  for root, dirs, files in os.walk(location):
      for item in fnmatch.filter(files, "*.md"):
          file_path = os.path.join(root, item)

          # Open the file and read the lines as a list
          with open(file_path) as f:
              lines = f.readlines()

          with open(file_path, 'w') as f:
              # Loop through the list of lines and titlecase
              # any line beginning with '#'.
              for line in lines:
                  if line.strip().startswith('#'):
                      line = titlecase(line)

if __name__ == "__main__":
  except IndexError: