Get total word count from WordPress blog with Python

I just came across this script by Jabba Laci to download your posts from WordPress.  It reminded me that I wanted to see how many words I have written to my blog so far.  Maybe it’s just because I’m still new to WordPress, but I haven’t seen a total word count anywhere. Here’s the modified script:

#!/usr/bin/env python
from __future__ import division
import xmlrpclib


server = xmlrpclib.ServerProxy('')
result = server.metaWeblog.getRecentPosts('YOUR_BLOG_ADDRESS', 'YOUR_USERNAME', 'YOUR_PASSWORD', MAX_POSTS)

numPosts = len(result)
numWords = 0
for post in result:
  numWords += len(post['description'].split())
avgWords = numWords/numPosts

print """Total Posts: %d
Total Words: %d
Avg Words: %d""" % (numPosts, numWords, avgWords)

And the output I get is:

Total Posts: 21
Total Words: 9731
Avg Words: 463

Which is slightly wrong because this simple script does not differentiate between drafts and posts (and my word counting routine is probably too naive among other things). A more correct and robust version of this is left as an exercise to the reader. I’m more than satisfied with this simple, rough estimate.



3 thoughts on “Get total word count from WordPress blog with Python

  1. Running the python script it will tell me that I only have 100 posts when in reality I have 191 currently. Do you have any advice on how to fix it to count all of them?

    • If you look on line 5 there is a setting for the maximum number of posts:

      MAX_POSTS = 100

      You will have to increase that value to be larger than the number of posts that you have. Otherwise the script will only give you the word count of the latest 100 posts.

  2. Awesome, I have been looking for a way to at least roughly estimate how much work I have put down on my blog for a while now. Thanks for posting!

Comments are closed.