简体   繁体   English

Python 从单词 vba 宏创建查找替换字典

[英]Python to create a find-replace dictionary from a word vba macro

I have a very big 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

I want to make a find replace dictionary from this vba macro我想从这个 vba 宏中创建一个查找替换字典

dictionary = {"asa":"fsa",
              "oriented":"das",
              "ampere":"^sasd",
              "wattage":"watts",
              "balanced dit":"BD",
              "Opthamologist":"Eyes",
              "goot health":"good health",}

the whole vba macro should be converted to this dictionary.整个 vba 宏应该被转换成这个字典。

the words from .text and .replacement text should be made in to a dictionary. .text 和 .replacement 文本中的单词应编入字典。

I need a python code to take the words from .TEXT and .Replacement Text and make a dictionary to我需要一个 python 代码来从 .TEXT 和 .Replacement Text 中获取单词并制作一个字典

"TEXT":"ReplacementText" "TEXT":"替换文本"

I hope I have explained my problem very well.我希望我已经很好地解释了我的问题。

import re

with open('source.txt', 'r') as file:
    inputText = file.readlines()

myDict = {}
for line in range(len(inputText)):
    # Have a Text line
    if inputText[line].lstrip().startswith('.Text'):
        # Get the key with regular expression
        key = re.search('"(.+?)"', inputText[line]).group(1)
        value = re.search('"(.+?)"', inputText[line + 1]).group(1)
        myDict[key] = value
print(myDict)

Output:输出:

{'asa': 'fsa', 'oriented': 'das', 'ampere': '^sasd', 'wattage': 'watts', 'balanced dit': 'BD', 'Opthamologist': 'Eyes', 'goot health': 'good health'}

use a regex to get everything inside quotes.使用正则表达式获取引号内的所有内容。 This will fetch key and value pairs of the wished output.这将获取所需输出的键和值对。 Get all keys and values by slice with step of size 2.按步长为 2 的切片获取所有键和值。

import re

text = # from file

regex = r'"([^"]+)"'

matches = re.findall(regex, text)

out = dict(zip(matches[::2], matches[1::2]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM