blob: de372d611017ba7257c661f3d0572e5bc435ee9a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)
|