编程语言
首页 > 编程语言> > python-使用pyUNO搜索字符串和换行符

python-使用pyUNO搜索字符串和换行符

作者:互联网

我想从文档中删除特定的字符串.我设法删除字符串的内容,但是换行符仍然保留.我发现了一些关于ControlCharacters的东西,但似乎它们只是数字常量.它真的有用吗?

这可行.

r = oDoc.createReplaceDescriptor()
r.setSearchString("FOOBAR")
r.setReplaceString("OTHERSTUFF")
oDoc.replaceAll(r)

这不

r = oDoc.createReplaceDescriptor()
r.setSearchString("FOOBAR\n")
r.setReplaceString("OTHERSTUFF")
oDoc.replaceAll(r)
r = oDoc.createReplaceDescriptor()
r.setSearchString("FOOBAR\r")
r.setReplaceString("OTHERSTUFF")
oDoc.replaceAll(r)

如何删除整行,包括换行符?

解决方法:

根据内置的帮助:

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.

我将其解释为无法搜索换行符.而是循环搜索结果并删除字符.这是执行此操作的一些代码:

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

标签:python,uno,pyuno
来源: https://codeday.me/bug/20191012/1901449.html