[Solved] How to detect non-breakable space in TextPortion

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
xenon325
Posts: 4
Joined: Sat Dec 09, 2017 3:38 pm

[Solved] How to detect non-breakable space in TextPortion

Post by xenon325 »

I have following paragraph (replaced non-breakable space with % here for clarity):
NORMAL11111111111111111111111111111111111111111111111111111111111111111111
non%breakable normal2
I iterate TextPortions in Python UNO and can get all the other character properties like weight, posture, color etc.
TextPortions are:
  • "NORMAL"
  • "1"
  • "1111111111111111111111111111111111111111111111111111111111111111111"
  • " non breakable normal2"
The line of code print(portion.CharKeepTogether) gives me an error: "AttributeError: CharKeepTogether".
And indeed, when I inspect all the properties of the portion with unohelper.inspect(portion, sys.stdout) (it prints out all I can get through the object), there is no CharKeepTogether in the list.

I've tried this with LibreOffice 5.3.0 and 5.4.2.2

Any ideas ?
Last edited by xenon325 on Sun Dec 10, 2017 9:47 pm, edited 2 times in total.
LibreOffice 5.3 / Windows 10
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to detect non-breakable space in TextPortion

Post by Lupp »

On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
j.kornelsn
Posts: 14
Joined: Wed Oct 06, 2010 6:26 am

Re: How to detect non-breakable space in TextPortion

Post by j.kornelsn »

As explained at https://ask.libreoffice.org/en/question ... -id-140300, apparently there is no longer a property named `CharKeepTogether`. Try the following code instead.

Code: Select all

def count_nonbreaking_spaces():
    oParEnum = XSCRIPTCONTEXT.getDocument().getText().createEnumeration()
    count = 0
    while oParEnum.hasMoreElements():
        oPar = oParEnum.nextElement()
        oPortionEnum = oPar.createEnumeration()
        while oPortionEnum.hasMoreElements():
            oTextPortion = oPortionEnum.nextElement()
            if oTextPortion.TextPortionType == "Text":
                s = oTextPortion.getString()
                for c in s:
                    if c == "\xA0":
                        count += 1
    print("Found %d nonbreaking spaces." % (count))
0xA0 is described at http://www.fileformat.info/info/unicode ... /index.htm.
LO / AOO on Ubuntu / Windows
xenon325
Posts: 4
Joined: Sat Dec 09, 2017 3:38 pm

Re: How to detect non-breakable space in TextPortion

Post by xenon325 »

It works, thanks, @j.kornelsn!

For reference, here's a full list of Unicode non-breaking chars: http://unicode.org/reports/tr14/#GL
LibreOffice 5.3 / Windows 10
Post Reply