python怎么实现替换词中的关键文字

  介绍

小编给大家分享一下python怎么实现替换词中的关键文字,希望大家阅读完这篇文章之后都有所收获、下面让我们一起去探讨吧!

<强>环境:Python3.6

本文主要是通过win32com操作的词,对单词中进行常用的操作。本文以替换为例,讲解一下如何使用python在词中使用“通配符模式”(类似于正则表达式)替换文本内容。

# !/usr/bin/env  python   #,- *安康;编码:utf-8  - *   import 操作系统   import  win32com   得到win32com.client  import 调度   ,   ,   #,处理词文档的类   ,   class  RemoteWord:   ,,,def  __init__(=没有自我,,文件名):   ,,,,,,,self.xlApp =, win32com.client.Dispatch (& # 39; Word.Application& # 39;), #,此处使用的是调度,原文中使用的DispatchEx会报的错   ,,,,,,,self.xlApp.Visible =, 0, #,后台运行,不显示   ,,,,,,,self.xlApp.DisplayAlerts =, 0,, #不警告   ,,,,,,,if 文件名:   ,,,,,,,,,,,self.filename =,文件名   ,,,,,,,,,,,if  os.path.exists (self.filename):   ,,,,,,,,,,,,,,,self.doc =, self.xlApp.Documents.Open(文件名)   ,,,,,,,,,,,其他的:   ,,,,,,,,,,,,,,,self.doc =, self.xlApp.Documents.Add(),, #,创建新的文档   ,,,,,,,,,,,,,,,self.doc.SaveAs(文件名)   ,,,,,,,其他的:   ,,,,,,,,,,,self.doc =, self.xlApp.Documents.Add ()   ,,,,,,,,,,,self.filename =, & # 39; & # 39;   ,   ,,,def  add_doc_end(自我,,字符串):   ,,,,,,,& # 39;& # 39;& # 39;在文档末尾添加内容& # 39;& # 39;& # 39;   ,,,,,,,rangee =, self.doc.Range ()   ,,,,,,,rangee.InsertAfter (& # 39; \ n # 39;, +,字符串)   ,   ,,,def  add_doc_start(自我,,字符串):   ,,,,,,,& # 39;& # 39;& # 39;在文档开头添加内容& # 39;& # 39;& # 39;   ,,,,,,,rangee =, self.doc.Range (0, 0)   ,,,,,,,rangee.InsertBefore(时间+ string  & # 39; \ n # 39;)   ,   ,,,def  insert_doc(自我,,insertPos,,字符串):   ,,,,,,,& # 39;& # 39;& # 39;在文档insertPos位置添加内容& # 39;& # 39;& # 39;   ,,,,,,,rangee =, self.doc.Range (0,, insertPos)   ,,,,,,,if  (insertPos ==, 0):   ,,,,,,,,,,,rangee.InsertAfter(字符串)   ,,,,,,,其他的:   ,,,,,,,,,,,rangee.InsertAfter (& # 39; \ n # 39;, +,字符串)   ,   ,,,def  replace_doc(字符串,自我,还以为;new_string):   ,,,,,,,& # 39;& # 39;& # 39;替换文字& # 39;& # 39;& # 39;   ,,,,,,,self.xlApp.Selection.Find.ClearFormatting ()   ,,,,,,,self.xlApp.Selection.Find.Replacement.ClearFormatting ()   ,,,,,,,#(字符串——搜索文本,   ,,,,,,,#,真的——区分大小写,   ,,,,,,,#,真的,完全匹配的单词,并非单词中的部分(全字匹配),   ,,,,,,,#,真的——使用通配符,   ,,,,,,,#,真的——同音,   ,,,,,,,#,真的——查找单词的各种形式,   ,,,,,,,#,真的——向文档尾部搜索,   ,,,,,,,#,1,   ,,,,,,,#,真的——带格式的文本,   ,,,,,,,#,new_string——替换文本,   ,,,,,,,#,2——替换个数(全部替换)   ,,,,,,,self.xlApp.Selection.Find.Execute(字符串,,假的,假的,,,,假的,,假的,,真的,,1,,真的,,new_string,, 2)   ,   ,,,def  replace_docs(字符串,自我,还以为;new_string):   ,,,,,,,& # 39;& # 39;& # 39;采用通配符匹配替换& # 39;& # 39;& # 39;   ,,,,,,,self.xlApp.Selection.Find.ClearFormatting ()   ,,,,,,,self.xlApp.Selection.Find.Replacement.ClearFormatting ()   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

python怎么实现替换词中的关键文字