You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AutoHotkey实现LibreOffice Writer多词查找替换(附Word示例)

适配LibreOffice Writer的AutoHotkey批量替换脚本及转换方法

原Word适配版AutoHotkey脚本

我有一段适配AutoHotkey的脚本,可在Word文档中遍历文本,从INI文件读取键值对批量替换指定词汇,对翻译工作帮助很大。原代码如下:

!k:: ; 热键:将指定键替换为对应值
oWord := ComObj("Word.Application")
TextRange := oWord.ActiveDocument.Range

TextRange.Find.ClearFormatting
TextRange.Find.Forward := true
TextRange.Find.Wrap := 1
TextRange.Find.Format := true
TextRange.Find.MatchCase := false
TextRange.Find.MatchWholeWord := false
TextRange.Find.MatchWildcards := false
TextRange.Find.MatchSoundsLike := false
TextRange.Find.MatchAllWordForms := false

TextRange.Find.Replacement.ClearFormatting
TextRange.Find.Replacement.Font.Bold := false
TextRange.Find.Replacement.Font.Underline := 0
TextRange.Find.Replacement.Font.Color := 0 ; 黑色字体
ini := A_ScriptDir "\zO.ini" ; 从INI文件读取键值对
IniRead txt, % ini, Blackflex ; 读取指定小节的内容
For each, line in StrSplit(txt, "`n") {
 part := StrSplit(line, "=") ; 键值对格式:key1=value1
 textRange.Find.Text             := part[1] ; 要查找的键
 textRange.Find.Replacement.Text := part[2] ; 替换的值
 textRange.Find.Execute(,,,,,,,,,, 2)
}

适配LibreOffice Writer的修改版脚本

LibreOffice使用UNO接口而非COM,因此需要调整对象获取和查找替换逻辑,以下是实现相同功能的脚本:

!k:: ; 热键:LibreOffice Writer批量键值替换
; 连接LibreOffice UNO服务
oServiceManager := ComObjCreate("com.sun.star.ServiceManager")
oDesktop := oServiceManager.createInstance("com.sun.star.frame.Desktop")
oDoc := oDesktop.getCurrentComponent()
if (!oDoc || oDoc.supportsService("com.sun.star.text.TextDocument")=false) {
    MsgBox, 请打开LibreOffice Writer文档后再执行
    return
}

oText := oDoc.getText()
oSearchDescriptor := oDoc.createSearchDescriptor()

; 设置查找参数
oSearchDescriptor.SearchForward := true
oSearchDescriptor.SearchWrap := 1 ; 对应com.sun.star.util.SearchWrap.WRAP_AROUND
oSearchDescriptor.SearchCaseSensitive := false
oSearchDescriptor.SearchWholeWords := false
oSearchDescriptor.SearchRegularExpression := false

; 设置替换格式
oReplaceDesc := oDoc.createReplaceDescriptor()
oReplaceDesc.assign(oSearchDescriptor) ; 继承查找参数
oReplaceDesc.ReplaceFormat := true
oCharProps := oReplaceDesc.getReplaceCharacterAttributes()
oCharProps.setPropertyValue("Bold", false)
oCharProps.setPropertyValue("Underline", 0) ; 对应com.sun.star.awt.FontUnderline.NONE
oCharProps.setPropertyValue("Color", 0) ; 黑色

; 读取INI文件中的键值对
ini := A_ScriptDir "\zO.ini"
IniRead txt, % ini, Blackflex
if (ErrorLevel) {
    MsgBox, 无法读取INI文件:%ini%
    return
}

For each, line in StrSplit(txt, "`n") {
    line := Trim(line)
    if (line = "" || InStr(line, ";")=1) ; 跳过空行和注释
        continue
    part := StrSplit(line, "=")
    if (part.Length() < 2)
        continue
    searchKey := Trim(part[1])
    replaceVal := Trim(part[2])
    
    oReplaceDesc.SearchString := searchKey
    oReplaceDesc.ReplaceString := replaceVal
    ; 执行全部替换
    oDoc.replaceAll(oReplaceDesc)
}
MsgBox, 批量替换完成
return

VBA/LibreOffice Basic转AutoHotkey的核心方法

  • 对象模型差异处理:

    • Word依赖COM接口,直接通过ComObj("Word.Application")获取实例;LibreOffice需通过UNO服务管理器创建com.sun.star命名空间下的组件,比如桌面服务、文档组件。
    • 查找替换逻辑:Word用Range.Find对象执行替换,LibreOffice需创建SearchDescriptor和ReplaceDescriptor,通过replaceAll方法完成批量替换。
  • 参数与属性映射:

    • 格式属性:Word直接修改Font对象属性,LibreOffice需通过getReplaceCharacterAttributes()获取属性集合,再用setPropertyValue设置对应格式。
    • 查找选项:如大小写匹配、整词匹配等,需对应LibreOffice的枚举常量数值(如循环查找对应SearchWrap.WRAP_AROUND的数值1)。
  • 错误边界处理:

    • 增加文档类型校验、文件读取错误判断,避免因环境异常(如未打开文档、INI文件不存在)导致脚本崩溃。

内容的提问来源于stack exchange,提问作者NotAColdSun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 06:09:58