summaryrefslogtreecommitdiffstats
path: root/scribus/plugins/scriptplugin/samples/wordcount.py
diff options
context:
space:
mode:
authorcraig <craig@11d20701-8431-0410-a711-e3c959e3b870>2012-01-01 11:40:09 +0000
committercraig <craig@11d20701-8431-0410-a711-e3c959e3b870>2012-01-01 11:40:09 +0000
commit7ed83b6c6666eb8b6b104c211ae7e52907350372 (patch)
tree4430b556abac0ad660a0aacf1887d77f85d8be02 /scribus/plugins/scriptplugin/samples/wordcount.py
downloadscribus-7ed83b6c6666eb8b6b104c211ae7e52907350372.tar.gz
scribus-7ed83b6c6666eb8b6b104c211ae7e52907350372.tar.xz
scribus-7ed83b6c6666eb8b6b104c211ae7e52907350372.zip
Branch 1.3.5 tree to 1.4.x tree, goodbye 1.3.x
git-svn-id: svn://scribus.net/branches/Version14x/Scribus@17163 11d20701-8431-0410-a711-e3c959e3b870
Diffstat (limited to 'scribus/plugins/scriptplugin/samples/wordcount.py')
-rw-r--r--scribus/plugins/scriptplugin/samples/wordcount.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/scribus/plugins/scriptplugin/samples/wordcount.py b/scribus/plugins/scriptplugin/samples/wordcount.py
new file mode 100644
index 0000000..de372d6
--- /dev/null
+++ b/scribus/plugins/scriptplugin/samples/wordcount.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+""" Counts the words in the whole document or in a textframe """
+
+import sys
+
+try:
+ from scribus import *
+except ImportError:
+ print "This script only runs from within Scribus."
+ sys.exit(1)
+
+import re
+
+TITLE = "Word count"
+
+def wordsplit(text):
+ word_pattern = "([A-Za-zäöüÄÖÜß]+)"
+ words = []
+ for x in re.split(word_pattern, text):
+ if re.match(word_pattern, x):
+ words.append(x)
+ return words
+
+
+def main():
+ words = 0
+ sel_count = selectionCount()
+ if sel_count:
+ source = "selected textframe"
+ if sel_count > 1: source += "s" #plural
+ for i in range(sel_count):
+ try:
+ text = getText(getSelectedObject(i))
+ words += len(wordsplit(text))
+ except WrongFrameTypeError:
+ if sel_count == 1:
+ # If there's only one object selected, display a message
+ messageBox(TITLE, "Can't count words in a non-text frame", ICON_INFORMATION);
+ sys.exit(1)
+ else:
+ # otherwise ignore
+ pass
+ else:
+ source = "whole document"
+ for page in range(1,pageCount() + 1):
+ gotoPage(page)
+ for obj in getAllObjects():
+ try:
+ text = getText(obj)
+ words += len(wordsplit(text))
+ except WrongFrameTypeError:
+ pass # ignore the error, it just wasn't a frame we can count
+
+ if words == 0: words = "No"
+ messageBox(TITLE, "%s words counted in %s" % (words, source),
+ ICON_INFORMATION)
+
+
+if __name__ == '__main__':
+ if haveDoc():
+ main()
+ else:
+ messageBox(TITLE, "No document open", ICON_WARNING)