Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to replace the document content of word operation in python

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report