createEnumeration problem
createEnumeration problem
Hi. I have a problem. For example, text document contains 5 paragraphs. I edit paragraphs in loop. I add 'new line' symbols chr(13) in some paragraphs. I got more than 5 paragraphs in result, but loop will have 5 iterations only and does not get all paragraphs. How i can solve this problem?
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
Re: createEnumeration problem
It would help if you could show the code you are using that does not perform as you expect it to.
Cheers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
David
OS - Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 24.2.2.2; SlackBuild for 24.2.2 by Eric Hameleers
Re: createEnumeration problem
robleyd, thanks for answer. My code[python] below:
This code edits text:
Paragraph 1
Paragraph 2
Paragraph 3
Paragraph 4
Paragraph 5
to:
Paragraph 1 prepared
test paragraph
Paragraph 2 prepared
test paragraph
Paragraph 3 prepared
test paragraph
Paragraph 4
Paragraph 5
Paragraphs 4 and 5 a not prepared.
Code: Select all
model = XSCRIPTCONTEXT.getDocument()
tCursor = model.getText().createTextCursor()
tCursor.gotoStart(False)
tCursor.gotoEnd(True)
eIterator = tCursor.createEnumeration()
while eIterator.hasMoreElements():
pCursor = eIterator.nextElement()
if pCursor.supportsService("com.sun.star.text.Paragraph") and pCursor.String != '':
pCursor.String = pCursor.String + " prepared" + chr(13) + "test paragraph"
return None
Paragraph 1
Paragraph 2
Paragraph 3
Paragraph 4
Paragraph 5
to:
Paragraph 1 prepared
test paragraph
Paragraph 2 prepared
test paragraph
Paragraph 3 prepared
test paragraph
Paragraph 4
Paragraph 5
Paragraphs 4 and 5 a not prepared.
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
-
- Posts: 145
- Joined: Mon Jun 13, 2016 10:50 am
Re: createEnumeration problem
Hello,
Try something like this:
or more pythonic (LibO 5.x only):
Try something like this:
Code: Select all
model = XSCRIPTCONTEXT.getDocument()
text = model.getText()
eIterator = text.createEnumeration()
while eIterator.hasMoreElements():
pCursor = eIterator.nextElement()
if pCursor.supportsService("com.sun.star.text.Paragraph") and pCursor.String != '':
pCursor.String = pCursor.String + " prepared" + chr(13) + "test paragraph"
Code: Select all
model = XSCRIPTCONTEXT.getDocument()
text = model.getText()
for elem in text:
if elem.supportsService("com.sun.star.text.Paragraph") and elem.String:
elem.String += " prepared\rtest paragraph"
Last edited by hubert lambert on Mon Jun 19, 2017 10:48 am, edited 1 time in total.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
Re: createEnumeration problem
Hi, hubert lambert. Method getText() returns all objects of document. I process selected textRanges only.
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
-
- Posts: 145
- Joined: Mon Jun 13, 2016 10:50 am
Re: createEnumeration problem
That was'nt obvious.Could you explain how you get then selection?karpo518 wrote:I process selected textRanges only.
Last edited by hubert lambert on Mon Jun 19, 2017 11:50 am, edited 1 time in total.
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
Re: createEnumeration problem
I got code from andrew pitonyak's book. I use paragraphs loop in function FormatWorker()
Code: Select all
...
model = XSCRIPTCONTEXT.getDocument()
oCurs = CreateSelectedTextIterator(model)
if oCurs is None :
msgBox("No selected text")
return None
for i in range(0, len(oCurs)):
FormatWorker(oCurs[i][0], oCurs[i][1], model, ".*")
...
def CreateSelectedTextIterator(oDoc):
if not IsAnythingSelected(oDoc):
return None
else:
oSels = oDoc.getCurrentSelection()
lSelCount = oSels.getCount()
oCurs = []
for lWhichSelection in range(0, lSelCount):
oCurs.append([])
oSel = oSels.getByIndex(lWhichSelection)
oLCurs = GetLeftMostCursor(oSel)
oRCurs = GetRightMostCursor(oSel)
oCurs[lWhichSelection].append(oLCurs)
oCurs[lWhichSelection].append(oRCurs)
return oCurs
def IsAnythingSelected(oDoc):
if(oDoc is None):
return False
oSels = oDoc.getCurrentSelection()
if(oSels is None) or (oSels.getCount() == 0):
return False
if (oSels.getCount() > 1):
return True
else:
oSel = oSels.getByIndex(0)
#msgBox(oSel.ImplementationName)
if oSel.supportsService("com.sun.star.text.XTextRange") :
msgBox("Поддержка текстовых интервалов")
oCursor = oSel.getText().createTextCursorByRange(oSel)
if not oCursor.isCollapsed():
return True
return False
def GetLeftMostCursor(oSel):
if oSel.getText().compareRegionStarts(oSel.getEnd(), oSel) >= 0 :
oRange = oSel.getEnd()
else:
oRange = oSel.getStart()
oCursor = oSel.getText().createTextCursorByRange(oRange)
oCursor.goRight(0, False)
return oCursor
def GetRightMostCursor(oSel):
if oSel.getText().compareRegionStarts(oSel.getEnd(), oSel) >= 0 :
oRange = oSel.getStart()
else:
oRange = oSel.getEnd()
oCursor = oSel.getText().createTextCursorByRange(oRange)
oCursor.goLeft(0, False)
return oCursor
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
-
- Posts: 145
- Joined: Mon Jun 13, 2016 10:50 am
Re: createEnumeration problem
Keeping a separate reference to paragraphs seems to work (could be a kind of bug):
Code: Select all
model = XSCRIPTCONTEXT.getDocument()
selection = model.CurrentSelection
elements = []
for element in selection[0]:
elements.append(element)
for element in elements:
if element.supportsService("com.sun.star.text.Paragraph") and element.String:
element.String += " prepared\rtest paragraph"
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
Re: createEnumeration problem
hubert lambert, thanck you. I will test this solution later.
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)
Re: createEnumeration problem
Does it work? If not then it might because you make an enumeration but while going through it you edit the document. Going in reverse order (backwards) might help. Also test if the selection range adapts if you are adding more paragraphs. The start should be the same but I'm not sure about the end.
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/li ... itect.html
Re: createEnumeration problem
Thanks for answers. The proposed method solves my problem.
musikai, it is works, but it is compromiss solution. I wanted to get ability to adding paragraph without problem in loop or dynamic initialization of the iterator
musikai, it is works, but it is compromiss solution. I wanted to get ability to adding paragraph without problem in loop or dynamic initialization of the iterator
Libre Office 5.1.6.2, Linux Mint 18 (64 bit)