Pywin32如何替换整个word文档中的文字

Pywin32 how to replace text in entire word document

我无法替换整个文本文档中的单词。以下代码用于替换主要段落中的单词,但在文本框中存在文本时不起作用。

wdFindContinue = 1
wdReplaceAll = 2
word.Selection.Find.Execute(find_str, True, True, False, False, False, True, wdFindContinue, False, replace_str, wdReplaceAll)

有没有办法替换整个文档中的单词?

请参阅 Word MVP 在 Using a macro to replace text wherever it appears in a document 上发表的文章。

A collaborative effort of MVP’s Doug Robbins and Greg Maxey with enhancements by Peter Hewett and Jonathan West

Using the Find or Replace utility on the Edit menu you can find or replace text "almost" anywhere it appears in the document. If you record that action however, the scope or "range" of the resulting recorded macro will only act on the text contained in the body of the document (or more accurately, it will only act on the part of the document that contains the insertion point). This means that if the insertion point is located in the main body of the document when your macro is executed it will have no effect on text that is in the headers or footers of the document, for example, or in a textbox, footnotes, or any other area that is outside the main body of the document.

Even the Find and Replace utility has a shortcoming. For example, text in a textbox located in a header or footer is outside the scope of the Find and Replace utility search range.

***

To use a macro to find or replace text anywhere in a document, it is necessary to loop through each individual part of the document. In VBA, these parts are called StoryRanges. Each StoryRange is identified by a unique wdStoryType constant.

There are eleven different wdStoryType constants that can form the StoryRanges (or parts) of a document (ok, a few more in later versions of Word, but they have no bearing in this discussion). Simple documents may contain only one or two StoryRanges, while more complex documents may contain more. The wdStoryTypes that have a role in find and replace are:

wdCommentsStory, wdEndnotesStory, wdEvenPagesFooterStory, wdEvenPagesHeaderStory, wdFirstPageFooterStory, wdFirstPageHeaderStory, wdFootnotesStory, wdMainTextStory, wdPrimaryFooterStory, wdPrimaryHeaderStory, and wdTextFrameStory.

The complete code to find or replace text anywhere is a bit complex. Accordingly, let’s take it a step at a time to better illustrate the process. In many cases the simpler code is sufficient for getting the job done.

第 1 步

The following code loops through each StoryRange in the active document and replaces the specified .Text with .Replacement.Text:

Sub FindAndReplaceFirstStoryOfEachType()
  Dim rngStory As Range
  For Each rngStory In ActiveDocument.StoryRanges
    With rngStory.Find
      .Text = "find text"
      .Replacement.Text = "I'm found"
      .Wrap = wdFindContinue
      .Execute Replace:=wdReplaceAll
    End With
  Next rngStory
End Sub

(Note for those already familiar with VBA: whereas if you use Selection.Find, you have to specify all of the Find and Replace parameters, such as .Forward = True, because the settings are otherwise taken from the Find and Replace dialog's current settings, which are “sticky”, this is not necessary if using [Range].Find – where the parameters use their default values if you don't specify their values in your code).

The simple macro above has shortcomings. It only acts on the "first" StoryRange of each of the eleven StoryTypes (i.e., the first header, the first textbox, and so on). While a document only has one wdMainTextStory StoryRange, it can have multiple StoryRanges in some of the other StoryTypes. If, for example, the document contains sections with un-linked headers and footers, or if it contains multiple textboxes, there will be multiple StoryRanges for those StoryTypes and the code will not act upon the second and subsequent StoryRanges. To even further complicate matters, if your document contains unlinked headers or footers and one of the headers or footers are empty then VBA can have trouble "jumping" that empty header or footer and process subsequent headers and footers.

页面内容更多,但以上应该有所帮助。