Skip to content

Instantly share code, notes, and snippets.

@rdapaz
Created June 4, 2020 06:17
Show Gist options
  • Save rdapaz/9cc34f7b6341f1a244b6dae7ee9ea837 to your computer and use it in GitHub Desktop.
Save rdapaz/9cc34f7b6341f1a244b6dae7ee9ea837 to your computer and use it in GitHub Desktop.
MS Word - Search and Replace
import win32com.client as c
import pprint
import re
class Util:
@staticmethod
def pretty_printer(o):
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(o)
class WordDocument:
def __init__(self, path):
self.path = path
self.wdApp = c.gencache.EnsureDispatch('Word.Application')
self.wdApp.Visible = True
self.doc = self.wdApp.Documents.Open(path)
self.txt = (self.doc.Content).Text.encode('ascii', errors='ignore')
self.txt = str(self.txt)
def getAllText(self, interestingText):
rex = re.compile(r"{}".format(interestingText))
entries = set()
for entry in rex.findall(self.txt):
entries.add(entry)
Util.pretty_printer(entries)
for entry in sorted(list(entries)):
print(entry)
def searchAndReplace(self):
subs = {
"Old": "New"
}
WDREPLACEALL = 2
for k, v in subs.items():
rge = self.doc.Content
rge.Find.Execute(FindText=k, ReplaceWith=subs[k], Replace=WDREPLACEALL)
if __name__ == '__main__':
path = r'<doc.docx'
doc = WordDocument(path)
# doc.getAllText(r'V(?:NGU|OKH)\w+\d{2}')
doc.searchAndReplace()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment