Home > Enterprise >  Python to create a find-replace dictionary from a word vba macro
Python to create a find-replace dictionary from a word vba macro

Time:06-06

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.

CodePudding user response:

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'}

CodePudding user response:

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]))
  • Related