简体   繁体   English

在python中使用win32com.client如何查找和替换多个文本

[英]using win32com.client in python how to find and replace multiple text

I'm trying to add multiple string values to a Word document using find and replace with the win32com.client Python library. 我正在尝试使用find并用win32com.client Python库替换将多个字符串值添加到Word文档中。

I can find and replace one value, but I don't know how to do this for multiple values. 我可以找到并替换一个值,但是我不知道如何对多个值执行此操作。

This is what I have so far: 这是我到目前为止的内容:

import win32com.client
word = win32com.client.DispatchEx("Word.Application")
word.Visible = True
word.DisplayAlerts = 0
word.Documents.Open("C:\TEMP\Testing\Me.docx")
word.Selection.Find
find.Text = "First Name"
find.Replacement.Text = "John"
find.Execute(Replace=1, Forward=True)

# the following part doesn't run
find.Text = "Last Name"             
find.Replacement.Text = "Smith"
find.Execute(Replace=1, Forward=True)

word.ActiveDocument.SaveAs('C:\TEMP\Testing\Me2.docx')
word.Quit() # releases Word object from memory

Any suggestions? 有什么建议么?

Try this: 尝试这个:

import win32com.client
from os import getcwd, listdir

docs = [i for i in listdir('.') if i[-3:]=='doc' or i[-4:]=='docx'] #All Word file

FromTo = {"First Name":"John",
          "Last Name":"Smith"} #You can insert as many as you want


word = win32com.client.DispatchEx("Word.Application")
word.Visible = True #Keep comment after tests
word.DisplayAlerts = False

for doc in docs:
    word.Documents.Open('{}\\{}'.format(getcwd(), doc))
    for From in FromTo.keys():
        word.Selection.Find.Text = From
        word.Selection.Find.Replacement.Text = FromTo[From]
        word.Selection.Find.Execute(Replace=2, Forward=True) #You made the mistake here=> Replace must be 2  
    name = doc.rsplit('.',1)[0]
    ext = doc.rsplit('.',1)[1]
    word.ActiveDocument.SaveAs('{}\\{}_2.{}'.format(getcwd(), name, ext))

word.Quit() # releases Word object from memory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 win32com.client 读取 MS Word 表格 - Using win32com.client to Read MS Word Table Python - 使用win32com.client接受Word文档中的所有更改 - Python - Using win32com.client to accept all changes in Word Documents 如何使用win32com.client api访问MS word的脚注 - How to access footnotes of MS word by using win32com.client api 使用win32com.client api打开MS Word 2016和访问formFileds时如何解决AttributeError? - How to resolve AttributeError when using win32com.client api for opening MS word 2016 and access formFileds? Python pywin32(win32com.client)在打开()后准备好Microsoft Word文档之前响应错误表 - Python pywin32 (win32com.client) responses wrong table before Microsoft Word document ready after open() 在Win32COM中查找和替换标题中的文本 - Find and Replace text within headers with Win32COM 使用Python win32com查找并替换Word文档中的斜体 - Find and replace italics in Word document with Python win32com Python win32com - 自动化Word - 如何替换文本框中的文本? - Python win32com - Automating Word - How to replace text in a text box? 无法使用win32com.client打开只读Microsoft Word文件 - Cannot open read-only Microsoft Word files with win32com.client 如何使用Python win32com将MS Word中的文本居中 - How to center text in MS Word with Python win32com
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM