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)文章
獲取django框架orm query執(zhí)行的sql語句實(shí)現(xiàn)方法分析
這篇文章主要介紹了獲取django框架orm query執(zhí)行的sql語句實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Django框架中orm query執(zhí)行的sql語句獲取方法相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-06-06
Python input函數(shù)使用實(shí)例解析
這篇文章主要介紹了Python input函數(shù)使用實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
Tensorflow 模型轉(zhuǎn)換 .pb convert to .lite實(shí)例
今天小編就為大家分享一篇Tensorflow 模型轉(zhuǎn)換 .pb convert to .lite實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
pyinstaller打包exe程序的步驟和添加依賴文件的實(shí)現(xiàn)
這篇文章主要介紹了pyinstaller打包exe程序的步驟和添加依賴文件的實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

