In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to replace the key words in word with python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to replace the key words in word with python.
Environment: Python3.6
The main purpose of this paper is to operate word through win32com and carry out common operations in word. Taking substitution as an example, this article shows how to use Python to replace text content with "wildcard patterns" (similar to regular expressions) in word.
#! / usr/bin/env python#-*-coding:utf-8-*-import osimport win32comfrom win32com.client import Dispatch# class dealing with Word documents class RemoteWord: def _ _ init__ (self, filename=None): self.xlApp = win32com.client.Dispatch ('Word.Application') # Dispatch is used here, and the DispatchEx used in the original text will report an error self.xlApp.Visible = 0 # run in the background Do not display self.xlApp.DisplayAlerts = 0 # do not warn if filename: self.filename = filename if os.path.exists (self.filename): self.doc = self.xlApp.Documents.Open (filename) else: self.doc = self.xlApp.Documents.Add () # create a new document self.doc.SaveAs (filename) else: self.doc = self.xlApp.Documents.Add () self.filename =''def add_doc_end (self String):''add content at the end of the document' 'rangee = self.doc.Range () rangee.InsertAfter ('\ n' + string) def add_doc_start (self, string):''add content at the beginning of the document' 'rangee = self.doc.Range (0,0) rangee.InsertBefore (string +'\ n') def insert_doc (self, insertPos, string):''add content at the insertPos location of the document' 'rangee = self.doc.Range (0) InsertPos) if (insertPos = = 0): rangee.InsertAfter (string) else: rangee.InsertAfter ('\ n'+ string) def replace_doc (self, string, new_string):''alternative text' 'self.xlApp.Selection.Find.ClearFormatting () self.xlApp.Selection.Find.Replacement.ClearFormatting () # (string-- search text, # True-- case sensitive, # True-- exact matching words Not part of the word (full word match), # True-- uses wildcards, # True-- homonym, # True-- finds various forms of words, # True-- searches the end of the document, # 1, # True-- formatted text, # new_string-- replacement text, # 2 True---number of substitutions (all replacements) self.xlApp.Selection.Find.Execute (string, False, True, 1 True, new_string, 2) def replace_docs (self, string, new_string):''replace' 'self.xlApp.Selection.Find.ClearFormatting () self.xlApp.Selection.Find.Replacement.ClearFormatting () self.xlApp.Selection.Find.Execute (string, False, False, True, False, 1, False, new_string, 2) def save (self):' save document 'self.doc.Save () def save_as (self) Filename):''save the document as' 'self.doc.SaveAs (filename) def close (self):' 'save the file, Close the file''self.save () self.xlApp.Documents.Close () self.xlApp.Quit () if _ _ name__ = =' _ _ main__': # path ='E:\\ XXX.docx' path = 'Elax.docx' doc = RemoteWord (path) # initialize a doc object # here demonstrates the replacement content Other functions themselves use doc.replace_doc ('','') # to replace the text content doc.replace_doc ('.','.) # as needed according to the functions of the above class. Doc.replace_doc ('\ n',') # remove the blank line doc.replace_doc ('oval penalty 0') # replace o with 0 # doc.replace_docs (' ([0-9]) @ [,] ([0-9]) @','\ 1.\ 2') use @ can't recognize and use {1,},\ need to use backslash to escape doc.replace_docs ('([0-9]) {1,} [,) .] ([0-9]) {1,}','\ 1.\ 2') # replace the,. Doc.replace_docs ('([0-9]) {1,} [old] ([0-9]) {1,}','\\ 101\\ 2') # replace "old" in the middle of the number with "01" doc.close ()
At this point, I believe you have a deeper understanding of "how to replace the key words in word with python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.