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

Keyboard macros or custom scripts

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

Postby xenon325 » Sat Dec 09, 2017 6:18 pm

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
xenon325
 
Posts: 4
Joined: Sat Dec 09, 2017 3:38 pm

Re: How to detect non-breakable space in TextPortion

Postby Lupp » Sat Dec 09, 2017 11:00 pm

On Windows 10: LibreOffice 6.1 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 1914
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to detect non-breakable space in TextPortion

Postby j.kornelsn » Sun Dec 10, 2017 10:40 am

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   Expand viewCollapse view
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
j.kornelsn
 
Posts: 13
Joined: Wed Oct 06, 2010 6:26 am

Re: How to detect non-breakable space in TextPortion

Postby xenon325 » Sun Dec 10, 2017 9:43 pm

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
xenon325
 
Posts: 4
Joined: Sat Dec 09, 2017 3:38 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 3 guests