[英]Python to create a find-replace dictionary from a word 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
我想從這個 vba 宏中創建一個查找替換字典
dictionary = {"asa":"fsa",
"oriented":"das",
"ampere":"^sasd",
"wattage":"watts",
"balanced dit":"BD",
"Opthamologist":"Eyes",
"goot health":"good health",}
整個 vba 宏應該被轉換成這個字典。
.text 和 .replacement 文本中的單詞應編入字典。
我需要一個 python 代碼來從 .TEXT 和 .Replacement Text 中獲取單詞並制作一個字典
"TEXT":"替換文本"
我希望我已經很好地解釋了我的問題。
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)
輸出:
{'asa': 'fsa', 'oriented': 'das', 'ampere': '^sasd', 'wattage': 'watts', 'balanced dit': 'BD', 'Opthamologist': 'Eyes', 'goot health': 'good health'}
使用正則表達式獲取引號內的所有內容。 這將獲取所需輸出的鍵和值對。 按步長為 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.