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

2024/11/16 15:53:53

I have a very big macro

Selection.Find.ClearFormattingSelection.Find.Replacement.ClearFormattingWith Selection.Find.Text = "asa".Replacement.Text = "fsa".Forward = True.Wrap = wdFindContinue.Format = False.MatchCase = False.MatchWholeWord = False.MatchWildcards = False.MatchSoundsLike = False.MatchAllWordForms = FalseEnd WithSelection.Find.Execute Replace:=wdReplaceAllWith Selection.Find.Text = "oriented".Replacement.Text = "das".Forward = True.Wrap = wdFindContinue.Format = False.MatchCase = False.MatchWholeWord = False.MatchWildcards = False.MatchSoundsLike = False.MatchAllWordForms = FalseEnd WithSelection.Find.Execute Replace:=wdReplaceAllWith Selection.Find.Text = "ampere".Replacement.Text = "^sasd".Forward = True.Wrap = wdFindContinue.Format = False.MatchCase = False.MatchWholeWord = False.MatchWildcards = False.MatchSoundsLike = False.MatchAllWordForms = FalseEnd WithSelection.Find.Execute Replace:=wdReplaceAllWith Selection.Find.Text = "wattage".Replacement.Text = "watts".Forward = True.Wrap = wdFindContinue.Format = False.MatchCase = False.MatchWholeWord = False.MatchWildcards = False.MatchSoundsLike = False.MatchAllWordForms = FalseEnd WithSelection.Find.Execute Replace:=wdReplaceAllWith Selection.Find.Text = "balanced dit".Replacement.Text = "BD".Forward = True.Wrap = wdFindContinue.Format = False.MatchCase = False.MatchWholeWord = False.MatchWildcards = False.MatchSoundsLike = False.MatchAllWordForms = FalseEnd WithSelection.Find.Execute Replace:=wdReplaceAllWith Selection.Find.Text = "Opthamologist".Replacement.Text = "Eyes".Forward = True.Wrap = wdFindContinue.Format = False.MatchCase = False.MatchWholeWord = False.MatchWildcards = False.MatchSoundsLike = False.MatchAllWordForms = FalseEnd WithSelection.Find.Execute Replace:=wdReplaceAllWith Selection.Find.Text = "goot health".Replacement.Text = "good health".Forward = True.Wrap = wdFindContinue.Format = False.MatchCase = False.MatchWholeWord = False.MatchWildcards = False.MatchSoundsLike = False.MatchAllWordForms = FalseEnd 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.

Answer
import rewith open('source.txt', 'r') as file:inputText = file.readlines()myDict = {}
for line in range(len(inputText)):# Have a Text lineif inputText[line].lstrip().startswith('.Text'):# Get the key with regular expressionkey = 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'}
https://en.xdnf.cn/q/120316.html

Related Q&A

Converting many .txt files into csv and combining them

I have many .txt files. I want to convert a few files ending with specific names into csv and combine them into one csv. ### Folder Name: text_files python_gramm.py aadd01.txt aaxx02.txt aaff03.txt hhd…

Calculation between two columns in Python?

When I tried to do some calculation between two columns (like division), I get an error: column_ratio[x]=(float(column1[y]))/(float(column2[z])) TypeError: tuple indices must be integers, not str. C…

Why does input() always return a string?

Here is my code:age = input("How old are you?: ") print (age) print (type(age))Result:How old are you?: 3535class str <<--- This is problem!But, If I use.. age = int(input("How …

Windowed mode cannot run

Why does pyinstaller exe not run in windowed mode but fine without it? I have changed over to a windows OS from Linux. Never had any issue before hand, how do I correct this.

inserting a variable into an fstring using .replace()

I have a code something similar to bellow. name = Dave message = f<name> is a really great guy! message = message.replace(<name>, {name}) print(message)the variables are a little more compl…

How to allow caps in this input box program for pygame?

I found this input box module on the internet but it only allows lower case no upper. So could someone tell me what to change in the module to allow caps as im creating a small multiplayer game and i n…

Why testing error rate increases at high values of K in KNN algorithm?

I am getting the error rates like this up to 20 values what might be the reason for this ?k_values: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] Error [0.0, 0.0, 0.0, 0.0, 0…

Divide and Conquer. Find the majority of element in array

I am working on a python algorithm to find the most frequent element in the list. def GetFrequency(a, element): return sum([1 for x in a if x == element])def GetMajorityElement(a):n = len(a)if n == …

Scraping dynamic webpage using Python

I am trying to scrape following dynamically generated webpage https://www.governmentjobs.com/careers/capecoral?page=1 Ive used requests, scrapy, scrapy-splash but I simply get page source code and I d…

numba cuda deprecation error : how to update my code?

Im running a jupyter notebook frome here : https://github.com/noahgift/nuclear_powered_command_line_tools/blob/master/notebooks/numba-cuda.ipynb The docs of current numba/cuda is here : https://numba.r…