python實(shí)現(xiàn)替換word中的關(guān)鍵文字(使用通配符)
更新時間:2020年02月13日 11:51:59 作者:漂泊者_(dá)LGD
今天小編就為大家分享一篇python實(shí)現(xiàn)替換word中的關(guān)鍵文字(使用通配符),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
環(huán)境:Python3.6
本文主要是通過win32com操作word,對word中進(jìn)行常用的操作。本文以替換為例,講解一下如何使用Python在word中使用“通配符模式”(類似于正則表達(dá)式)替換文本內(nèi)容。
#!/usr/bin/env python # -*- coding:utf-8 -*- import os import win32com from win32com.client import Dispatch # 處理Word文檔的類 class RemoteWord: def __init__(self, filename=None): self.xlApp = win32com.client.Dispatch('Word.Application') # 此處使用的是Dispatch,原文中使用的DispatchEx會報錯 self.xlApp.Visible = 0 # 后臺運(yùn)行,不顯示 self.xlApp.DisplayAlerts = 0 #不警告 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() # 創(chuàng)建新的文檔 self.doc.SaveAs(filename) else: self.doc = self.xlApp.Documents.Add() self.filename = '' def add_doc_end(self, string): '''在文檔末尾添加內(nèi)容''' rangee = self.doc.Range() rangee.InsertAfter('\n' + string) def add_doc_start(self, string): '''在文檔開頭添加內(nèi)容''' rangee = self.doc.Range(0, 0) rangee.InsertBefore(string + '\n') def insert_doc(self, insertPos, string): '''在文檔insertPos位置添加內(nèi)容''' rangee = self.doc.Range(0, insertPos) if (insertPos == 0): rangee.InsertAfter(string) else: rangee.InsertAfter('\n' + string) def replace_doc(self, string, new_string): '''替換文字''' self.xlApp.Selection.Find.ClearFormatting() self.xlApp.Selection.Find.Replacement.ClearFormatting() #(string--搜索文本, # True--區(qū)分大小寫, # True--完全匹配的單詞,并非單詞中的部分(全字匹配), # True--使用通配符, # True--同音, # True--查找單詞的各種形式, # True--向文檔尾部搜索, # 1, # True--帶格式的文本, # new_string--替換文本, # 2--替換個數(shù)(全部替換) self.xlApp.Selection.Find.Execute(string, False, False, False, False, False, True, 1, True, new_string, 2) def replace_docs(self, string, new_string): '''采用通配符匹配替換''' self.xlApp.Selection.Find.ClearFormatting() self.xlApp.Selection.Find.Replacement.ClearFormatting() self.xlApp.Selection.Find.Execute(string, False, False, True, False, False, False, 1, False, new_string, 2) def save(self): '''保存文檔''' self.doc.Save() def save_as(self, filename): '''文檔另存為''' self.doc.SaveAs(filename) def close(self): '''保存文件、關(guān)閉文件''' self.save() self.xlApp.Documents.Close() self.xlApp.Quit() if __name__ == '__main__': # path = 'E:\\XXX.docx' path = 'E:/XXX.docx' doc = RemoteWord(path) # 初始化一個doc對象 # 這里演示替換內(nèi)容,其他功能自己按照上面類的功能按需使用 doc.replace_doc(' ', '') # 替換文本內(nèi)容 doc.replace_doc('.', '.') # 替換.為. doc.replace_doc('\n', '') # 去除空行 doc.replace_doc('o','0') # 替換o為0 # doc.replace_docs('([0-9])@[、,,]([0-9])@', '\1.\2') 使用@不能識別改用{1,},\需要使用反斜杠轉(zhuǎn)義 doc.replace_docs('([0-9]){1,}[、,,.]([0-9]){1,}', '\\1.\\2') # 將數(shù)字中間的,,、.替換成. doc.replace_docs('([0-9]){1,}[舊]([0-9]){1,}', '\\101\\2') # 將數(shù)字中間的“舊”替換成“01” doc.close()
以上這篇python實(shí)現(xiàn)替換word中的關(guān)鍵文字(使用通配符)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python datetime 格式化 明天,昨天實(shí)例
這篇文章主要介紹了Python datetime 格式化 明天,昨天實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03python單線程文件傳輸?shù)膶?shí)例(C/S)
今天小編就為大家分享一篇python單線程文件傳輸?shù)膶?shí)例(C/S),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02Python實(shí)現(xiàn)隨機(jī)森林回歸與各自變量重要性分析與排序
這篇文章主要為大家詳細(xì)介紹了在Python環(huán)境中,實(shí)現(xiàn)隨機(jī)森林(Random Forest,RF)回歸與各自變量重要性分析與排序的過程,感興趣的小伙伴可以了解一下2023-02-02