Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to replace the document content of word operation in python

Shulou Source: shulou.com Published: 2022-06-02 03:27:05 09月15日 Update

How to replace the document content of word operation in python. In order to solve this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Sometimes, we need to batch replace specific content in word with new content, so how to apply python win32com to achieve batch replacement of word document content? Here's how to implement it.

Content search and new content replacement are mainly carried out through the find and replace method Find.Execute:

Replace the code:

Import os

Import win32com.clientdocx_path = 'test.docx'

App = win32com.client.DispatchEx ("Kwps.Application")

# app.Visible = True

# app.ScreenUpdating = Tru

Doc = app.Documents.Open (os.path.abspath (docx_path), ReadOnly=0)

Oldstr = "abcd"

Newstr = "1234"

App.Selection.Find.Execute (

Oldstr, False, True, 1, False, newstr, 2

)

Doc.Close ()

App.Quit ()

''

The 11 parameters involved are described:

(keywords for OldStr-- search

True-- is case sensitive

A word that matches exactly with True--, not a part of the word (full word match)

True-- uses wildcards

True-- homonym

True-- looks up various forms of words

True-- searches the end of the document

one,

True-- formatted text

NewStr-- replacement text

2mer-number of replacements (0 means no replacement, 1 means only the first match, 2 means all)

''

This is the answer to the document content replacement question on how to operate word in python. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Content documents words questions text methods more help search answer easy easy number code keywords keywords parameters size boys buddies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Apple Shulou Information Docker