Python-libreofficecalc-Find&;替换为正则表达式
我尝试在LibreOffice的Calc中用Python编写一个Find&Replace方法,用“&”替换所有的“+”(在一列中-不太重要)-不幸的是,即使是标准的Find&Replace方法(对我来说)似乎也不可能。这就是我目前的情况: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
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相同的字符串
所以替换字符串需要是文本--
\&
我对代码做了一些小的更改,但总而言之,它工作得非常好。非常感谢你!