Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-libreofficecalc-Find&;替换为正则表达式_Python_Libreoffice_Libreoffice Calc_Uno - Fatal编程技术网

Python-libreofficecalc-Find&;替换为正则表达式

Python-libreofficecalc-Find&;替换为正则表达式,python,libreoffice,libreoffice-calc,uno,Python,Libreoffice,Libreoffice Calc,Uno,我尝试在LibreOffice的Calc中用Python编写一个Find&Replace方法,用“&”替换所有的“+”(在一列中-不太重要)-不幸的是,即使是标准的Find&Replace方法(对我来说)似乎也不可能。这就是我目前的情况: import uno def search() desktop = XSCRIPTCONTEXT.getDesktop() document = XSCRIPTCONTEXT.getDocument() ctx = uno.getCom

我尝试在LibreOffice的Calc中用Python编写一个Find&Replace方法,用“&”替换所有的“+”(在一列中-不太重要)-不幸的是,即使是标准的Find&Replace方法(对我来说)似乎也不可能。这就是我目前的情况:

import uno
def search()
    desktop = XSCRIPTCONTEXT.getDesktop()
    document = XSCRIPTCONTEXT.getDocument()
    ctx = uno.getComponentContext()
    sm = ctx.ServiceManager
    dispatcher = sm.createInstanceWithContext("com.sun.star.frame.DispatchHelper", ctx)
    model = desktop.getCurrentComponent()
    doc = model.getCurrentController()
    sheet = model.Sheets.getByIndex(0)

    replace = sheet.createReplaceDescriptor()
    replace.SearchRegularExpression = True
    replace.SearchString = ".+$"
    replace.ReplaceString ="&"
    return None

结果是:什么都没有!我会很高兴并感谢每一个提示,样本代码和激励的话

此代码将A列中的所有非空单元格更改为
&

def calc_search_and_replace():
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()
    sheet = model.Sheets.getByIndex(0)
    COLUMN_A = 0
    cellRange = sheet.getCellRangeByPosition(COLUMN_A, 0, COLUMN_A, 65536);
    replace = cellRange.createReplaceDescriptor()
    replace.SearchRegularExpression = True
    replace.SearchString = r".+$"
    replace.ReplaceString = r"\&"
    cellRange.replaceAll(replace)
请注意,代码调用实际执行某些操作。此外,从:

&将插入与搜索RegExp相同的字符串


所以替换字符串需要是文本--
\&

我对代码做了一些小的更改,但总而言之,它工作得非常好。非常感谢你!