怎么用python替換word中的關(guān)鍵文字

本篇內(nèi)容主要講解“怎么用python替換word中的關(guān)鍵文字”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么用python替換word中的關(guān)鍵文字”吧!

墾利ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

環(huán)境:Python3.6

本文主要是通過win32com操作word,對word中進行常用的操作。本文以替換為例,講解一下如何使用Python在word中使用“通配符模式”(類似于正則表達式)替換文本內(nèi)容。

#!/usr/bin/env python# -*- coding:utf-8 -*-import osimport win32comfrom 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 # 后臺運行,不顯示    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替換word中的關(guān)鍵文字”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

分享文章:怎么用python替換word中的關(guān)鍵文字
文章路徑:http://bm7419.com/article26/jdihcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站外貿(mào)建站、服務器托管網(wǎng)站改版、網(wǎng)站營銷動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)