简体   繁体   中英

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

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.

the words from .text and .replacement text should be made in to a dictionary.

I need a python code to take the words from .TEXT and .Replacement Text and make a dictionary to

"TEXT":"ReplacementText"

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.

import re

text = # from file

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

matches = re.findall(regex, text)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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