Search strings and line breaks with pyUNO

后端 未结 1 522
鱼传尺愫
鱼传尺愫 2021-01-24 23:39

I would like to delete a specific string from a document. I manage to delete the content of the string, but the line break still remains after. I found some things about Control

相关标签:
1条回答
  • 2021-01-25 00:13

    According to the built in help:

    A search using a regular expression will work only within one paragraph. To search using a regular expression in more than one paragraph, do a separate search in each paragraph.

    I interpret this to mean that newline characters cannot be searched for. Instead, loop through the search results and delete the character. Here is some code that does this:

    search = oDoc.createSearchDescriptor()
    search.SearchRegularExpression = True
    search.SearchString = "FOOBAR$"
    selsFound = oDoc.findAll(search)
    for sel_index in range(0, selsFound.getCount()):
        oSel = selsFound.getByIndex(sel_index)
        try:
            oCursor = oSel.getText().createTextCursorByRange(oSel)
        except (RuntimeException, IllegalArgumentException):
            return
        oCursor.setString("")  # delete
        oCursor.goRight(1, True) # select newline character
        oCursor.setString("")  # delete
    
    0 讨论(0)
提交回复
热议问题