Python实现解析Word VBA查找替换宏生成替换字典
Word VBA查找替换规则提取方案
直接通过正则匹配VBA代码中成对的查找、替换文本参数,无需逐行解析复杂语法,适配批量With结构的Word宏代码,提取结果和VBA实际执行的替换规则完全一致。
实现代码
import re def extract_replace_dict(vba_code: str) -> dict: # 匹配.Text和对应.Replacement.Text的引号内内容,兼容不同缩进、空格写法 rule_pattern = re.compile( r'\.Text\s*=\s*"([^"]*)"\s*\n\s*\.Replacement\.Text\s*=\s*"([^"]*)"', re.M ) return {find: replace for find, replace in rule_pattern.findall(vba_code)} def print_target_format(replace_dict: dict) -> None: # 按指定格式打印输出字典 print("dictionary = {") item_list = list(replace_dict.items()) for index, (key, val) in enumerate(item_list): print(f'{" "*14}"{key}":"{val}",') print("}") if __name__ == "__main__": # 把完整VBA宏代码粘贴到下方三引号之间 vba_macro = ''' Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "asa" .Replacement.Text = "fsa" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll With Selection.Find .Text = "oriented" .Replacement.Text = "das" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll With Selection.Find .Text = "ampere" .Replacement.Text = "^sasd" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll With Selection.Find .Text = "wattage" .Replacement.Text = "watts" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll With Selection.Find .Text = "balanced dit" .Replacement.Text = "BD" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll With Selection.Find .Text = "Opthamologist" .Replacement.Text = "Eyes" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Replace:=wdReplaceAll With Selection.Find .Text = "goot health" .Replacement.Text = "good health" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With ''' result = extract_replace_dict(vba_macro) print_target_format(result)
使用说明
- 将自己的全量VBA宏代码替换掉示例里
vba_macro三引号包裹的内容即可 - 代码自动兼容不同缩进、空格写法,支持提取带空格、Word特殊查找标记(比如
^s这类^开头的符号)的文本 - 若VBA中存在重复的查找文本,默认保留最后一条替换规则,和VBA实际执行逻辑一致
- 运行后直接输出要求格式的字典内容,不需要额外调整格式
运行输出示例
针对给出的示例VBA,运行后输出内容和目标格式完全一致:
dictionary = { "asa":"fsa", "oriented":"das", "ampere":"^sasd", "wattage":"watts", "balanced dit":"BD", "Opthamologist":"Eyes", "goot health":"good health", }
内容的提问来源于stack exchange,提问作者Python-lover
相关产品推荐
相关产品推荐

