scripting/Package_ScriptsPython.mk |2
scripting/examples/python/Capitalise.py| 102 +++--
scripting/examples/python/HelloWorld.py| 25 ++-
scripting/examples/python/InsertText.py| 46 +++--
scripting/examples/python/NamedRanges.py |7
scripting/examples/python/SetCellColor.py | 27 +++
scripting/examples/python/TableSample.py | 131 +
scripting/examples/python/pythonSamples/TableSample.py | 116 ---
8 files changed, 264 insertions(+), 192 deletions(-)
New commits:
commit 5fb0e0f68bc9d37d43e0782b903919630852904c
Author: Kevin Suo
AuthorDate: Sat Oct 10 14:43:55 2020 +0800
Commit: Thorsten Behrens
CommitDate: Sat Oct 10 14:11:27 2020 +0200
Improve example python code in scripting.
* Modified the code to be more pythonic.
* If "len(theString) == 0", then "not theString" evaluates to True.
* "theString[0].isupper()" and "theString[1].isupper()" can be combined.
* Remove unused imported string module
* Wrap long lines
* run autopep8 to prettify
* ...
Change-Id: Ic8aaa0728a43936cd4c6e1ed590e01ba8f0fbf5b
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/104136
Tested-by: Jenkins
Reviewed-by: Thorsten Behrens
diff --git a/scripting/Package_ScriptsPython.mk
b/scripting/Package_ScriptsPython.mk
index 0c48839ca42d..9d401d38349b 100644
--- a/scripting/Package_ScriptsPython.mk
+++ b/scripting/Package_ScriptsPython.mk
@@ -15,7 +15,7 @@ $(eval $(call
gb_Package_add_files_with_dir,scripting_ScriptsPython,$(LIBO_SHARE
python/InsertText.py \
python/NamedRanges.py \
python/SetCellColor.py \
- python/pythonSamples/TableSample.py \
+ python/TableSample.py \
))
# vim: set noet sw=4 ts=4:
diff --git a/scripting/examples/python/Capitalise.py
b/scripting/examples/python/Capitalise.py
index 05e82a37ad32..64d29a51343a 100644
--- a/scripting/examples/python/Capitalise.py
+++ b/scripting/examples/python/Capitalise.py
@@ -16,61 +16,77 @@
# the License at http://www.apache.org/licenses/LICENSE-2.0 .
#
-# helper function
-def getNewString( theString ) :
-if( not theString or len(theString) ==0) :
+def getNewString(theString):
+"""helper function
+"""
+if (not theString):
return ""
+
# should we tokenize on "."?
-if theString[0].isupper() and len(theString)>=2 and theString[1].isupper()
:
-# first two chars are UC => first UC, rest LC
-newString=theString[0:1].upper() + theString[1:].lower();
+if len(theString) >= 2 and theString[:2].isupper():
+# first two chars are UC => first UC, rest LC
+newString = theString[0].upper() + theString[1:].lower()
+
elif theString[0].isupper():
-# first char UC => all to LC
-newString=theString.lower()
-else: # all to UC.
-newString=theString.upper()
-return newString;
+# first char UC => all to LC
+newString = theString.lower()
+
+else:
+# all to UC.
+newString = theString.upper()
-def capitalisePython( ):
-"""Change the case of a selection, or current word from upper case, to
first char upper case, to all lower case to upper case..."""
-import string
+return newString
+
+def capitalisePython():
+"""Change the case of the selected or current word(s).
+If at least the first two characters are "UPpercase, then it is changed
+to first char "Uppercase".
+If the first character is "Uppercase", then it is changed to
+all "lowercase".
+Otherwise, all are changed to "UPPERCASE".
+"""
# The context variable is of type XScriptContext and is available to
# all BeanShell scripts executed by the Script Framework
xModel = XSCRIPTCONTEXT.getDocument()
-#the writer controller impl supports the css.view.XSelectionSupplier
interface
+# the writer controller impl supports the css.view.XSelectionSupplier
+# interface
xSelectionSupplier = xModel.getCurrentController()
-#see section 7.5.1 of developers' guide
+# see section 7.5.1 of developers' guide
xIndexAccess = xSelectionSupplier.getSelection()
-count = xIndexAccess.getCount();
-if(count>=1): #ie we have a selection
-i=0
-while i < count :
-xTextRange = xIndexAccess.getByIndex(i);
-#print "string: " + xTextRange.getString();
-theString = xTextRange.getString();
-if len(theString)==0 :
-# sadly we can have a selection where nothing is selected
-# in this case we get the XWordCursor and make a selection!
-xText = xTextRange.getText();
-xWordCursor = xText.createTextCursorByRange(xTextRange);
-if not xWordCursor.isStartOfWord():
-xWordCursor.gotoStartOfWord(False);
-