天天看点

python替换word内容_怎么用python使得替换word中关键字

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

#!/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 # 后台运行,不显示

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() # 创建新的文档

self.doc.SaveAs(filename)

else:

self.doc = self.xlApp.Documents.Add()

self.filename = ''

def add_doc_end(self, string):

'''在文档末尾添加内容'''

rangee = self.doc.Range()

rangee.InsertAfter('\n' + string)

def add_doc_start(self, string):

'''在文档开头添加内容'''

rangee = self.doc.Range(0, 0)

rangee.InsertBefore(string + '\n')

def insert_doc(self, insertPos, string):

'''在文档insertPos位置添加内容'''

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--区分大小写,

# True--完全匹配的单词,并非单词中的部分(全字匹配),

# True--使用通配符,

# True--同音,

# True--查找单词的各种形式,

# True--向文档尾部搜索,

# 1,

# True--带格式的文本,

# new_string--替换文本,

# 2--替换个数(全部替换)

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):

'''保存文件、关闭文件'''

self.save()

self.xlApp.Documents.Close()

self.xlApp.Quit()

if __name__ == '__main__':

# path = 'E:\\XXX.docx'

path = '‪C:\\Users\\乐\\Desktop\\test1.docx'

doc = RemoteWord(path) # 初始化一个doc对象

# 这里演示替换内容,其他功能自己按照上面类的功能按需使用

doc.replace_doc('1', '2') # 替换文本内容

doc.replace_doc('.', '.') # 替换.为.

doc.replace_doc('\n', '') # 去除空行

doc.replace_doc('o','0') # 替换o为0

doc.replace_docs('([0-9])@[、,,]([0-9])@', '\1.\2') #使用@不能识别改用{1,},\需要使用反斜杠转义

doc.replace_docs('([0-9]){1,}[、,,.]([0-9]){1,}', '\\1.\\2') # 将数字中间的,,、.替换成.

doc.replace_docs('([0-9]){1,}[旧]([0-9]){1,}', '\\101\\2') # 将数字中间的“旧”替换成“01”

doc.close()

在VS里面 运行

出现以下错误

raceback (most recent call last):

File "c:/Users/乐/Desktop/test/word替换.py", line 85, in

doc = RemoteWord(path) # 初始化一个doc对象

File "c:/Users/乐/Desktop/test/word替换.py", line 21, in __init__

self.doc.SaveAs(filename)

File "", line 5, in SaveAs

pywintypes.com_error: (-2147352567, '发生意外。', (0, 'Microsoft Word', '这不是有效文件名。\n请试用下列方法: \n* 检查路径,确认键入无误。\n* 从文件和文件夹列表中选择

文件。', 'wdmain11.chm', 24632, -2146823136), None)

真的搞不懂是文件路径错误还是是 想把word文档中1 替换为 2

大神们指点一下吧 谢谢 或者有更好的替换方法麻烦指教一下 谢谢大神们