Page 1 of 1

Count Lines, Words and Characters

Posted: Wed Jun 06, 2012 8:12 am
by jussij
A simple Python macro to count the lines, words and characters in the current file.

Code: Select all

import zeus
import re

def key_macro():
    numWords = 0
    numChars = 0
    numCount = 0

    # macro only works for documents
    document = zeus.is_document()

    if (document == 1):
        zeus.screen_update_disable()

        numCount = zeus.get_line_count()

        for i in range(1, numCount):
            line = zeus.get_line_text(i)

            for word in re.findall("[a-zA-Z0-9]+", line):
                numWords += 1
                numChars += len(word)

    message = "Number of characters: %d \nNumber of words: %d \nNumber of lines: %d" % (numWords, numChars, numCount)

    zeus.message_box(0, message)

key_macro() # run the macro