| View previous topic :: View next topic |
| Author |
Message |
The gray Cardinal General User

Joined: 19 Oct 2007 Posts: 5
|
Posted: Sun Oct 21, 2007 1:08 am Post subject: [Writer] Python & OOo: Capitalize |
|
|
Macro Capitalise.py is distributed with OpenOffice v2.3. But I don't like it much because of the following reasons:
1. It doesn't work with the last word in a file, when there isn't any text or line feed after this word.
2. If you work with a word that has caret within it (not with a selected piece of text), for some reason Capitalise.py selects this word after processing.
3. If you deal with some non-adjacent selected pieces of text inside a document, a runtime exception occurs.
4. If you deal with a word that contains the caret and stands at the end of a line, after which there is a blank line, Capitalise.py adds another blank line.
Here is my version of the Capitalise.py, free of the above peculiarities:
| Code: | # copyleft © The gray Cardinal, Russia, http://script-coding.info/
def CapitalizeString(theString):
# Capitalize function
if (not theString or len(theString) == 0):
return ""
if theString[0].isupper() and len(theString) >= 2 and theString[1].isupper():
newString = theString[0:1].upper() + theString[1:].lower()
elif theString[0].isupper():
newString = theString.lower()
else:
newString = theString.upper()
import re
newString = re.sub('\n', '', newString)
return newString
def Capitalize(dummy = ''):
'''
Macro for Writer.
Changes the case of the selected pieces of text or the word with the caret within it.
'''
xController = XSCRIPTCONTEXT.getDocument().getCurrentController()
# current selection
xIndexAccess = xController.getSelection()
# number of selected pieces of text
count = xIndexAccess.getCount()
if count == 1 and len(xIndexAccess.getByIndex(0).getString()) == 0:
# No selection. Process the word with the caret within it.
xTextRange = xIndexAccess.getByIndex(0)
xWordCursor = xTextRange.getText().createTextCursorByRange(xTextRange)
if not xWordCursor.isStartOfWord(): xWordCursor.gotoStartOfWord(False)
xWordCursor.gotoEndOfWord(True)
theString = xWordCursor.getString()
# processing of the text
newString = CapitalizeString(theString)
# replace the text by the processed text
xWordCursor.setString(newString)
else:
# Selection occurred. Now process all the selected pieces of text.
i = 0
while i < count:
# selected piece of text
xTextRange = xIndexAccess.getByIndex(i)
# text of selected piece of text
theString = xTextRange.getString()
if len(theString) > 0:
# process the text
newString = CapitalizeString(theString)
# replace the text by the processed text
xTextRange.setString(newString)
# selection
xController.select(xTextRange)
# If some non-adjacent pieces of text were selected (by Ctrl),
# only the selection of the last piece of text will be restored.
# Unfortunately, there is no way to restore selection of all the pieces of text.
i += 1
return None
g_exportedScripts = BDS, Capitalize |
|
|
| Back to top |
|
 |
JZA OOo Advocate


Joined: 01 Feb 2003 Posts: 432 Location: Mexico
|
Posted: Sun Oct 21, 2007 12:18 pm Post subject: |
|
|
you should submit it as a patch for the udk.openoffice.org / api.openoffice.org project. I would like to know if you have any basic GUI programming from PyUNO. Thanks for the snippet. _________________ Alexandro Colorado
PPMC Apache OpenOffice
http://es.openoffice.org |
|
| Back to top |
|
 |
JZA OOo Advocate


Joined: 01 Feb 2003 Posts: 432 Location: Mexico
|
|
| Back to top |
|
 |
The gray Cardinal General User

Joined: 19 Oct 2007 Posts: 5
|
|
| Back to top |
|
 |
JZA OOo Advocate


Joined: 01 Feb 2003 Posts: 432 Location: Mexico
|
Posted: Mon Oct 22, 2007 12:29 pm Post subject: |
|
|
I think you choose the wrong project. The one I sent you was on the Framework project and scripting subcomponent.
I will fix it for you.
Update: Sorry for that, I dont have the permissions to re-assign it to
Component: Framework
subcomponent: scripting
OS: ALL
OOo Version: 2.3 _________________ Alexandro Colorado
PPMC Apache OpenOffice
http://es.openoffice.org |
|
| Back to top |
|
 |
|