我可以使用 Win32 COM 替换 Word 文档中的文本吗?

发布于 2024-07-25 07:18:44 字数 195 浏览 2 评论 0原文

我必须在某些文档中执行大量替换,问题是,我希望能够自动执行该任务。 一些文档包含通用字符串,如果可以自动化,这将非常有用。 根据我到目前为止所读到的内容,COM 可能是执行此操作的一种方法,但我不知道是否支持文本替换。 我希望能够在 python 中执行此任务? 是否可以? 您可以发布一段代码片段来显示如何访问文档的文本吗?

谢谢!

I have to perform a large number of replacements in some documents, and the thing is, I would like to be able to automate that task. Some of the documents contain common strings, and this would be pretty useful if it could be automated. From what I read so far, COM could be one way of doing this, but I don't know if text replacement is supported.
I'd like to be able to perform this task in python? Is it possible? Could you post a code snippet showing how to access the document's text?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

素染倾城色 2024-08-01 07:18:44

我喜欢到目前为止的答案;
这是一个经过测试的示例(从此处稍作修改)
替换 Word 文档中所有出现的字符串:

import win32com.client

def search_replace_all(word_file, find_str, replace_str):
    ''' replace all occurrences of `find_str` w/ `replace_str` in `word_file` '''
    wdFindContinue = 1
    wdReplaceAll = 2

    # Dispatch() attempts to do a GetObject() before creating a new one.
    # DispatchEx() just creates a new one. 
    app = win32com.client.DispatchEx("Word.Application")
    app.Visible = 0
    app.DisplayAlerts = 0
    app.Documents.Open(word_file)

    # expression.Execute(FindText, MatchCase, MatchWholeWord,
    #   MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, 
    #   Wrap, Format, ReplaceWith, Replace)
    app.Selection.Find.Execute(find_str, False, False, False, False, False, \
        True, wdFindContinue, False, replace_str, wdReplaceAll)
    app.ActiveDocument.Close(SaveChanges=True)
    app.Quit()

f = 'c:/path/to/my/word.doc'
search_replace_all(f, 'string_to_be_replaced', 'replacement_str')

I like the answers so far;
here's a tested example (slightly modified from here)
that replaces all occurrences of a string in a Word document:

import win32com.client

def search_replace_all(word_file, find_str, replace_str):
    ''' replace all occurrences of `find_str` w/ `replace_str` in `word_file` '''
    wdFindContinue = 1
    wdReplaceAll = 2

    # Dispatch() attempts to do a GetObject() before creating a new one.
    # DispatchEx() just creates a new one. 
    app = win32com.client.DispatchEx("Word.Application")
    app.Visible = 0
    app.DisplayAlerts = 0
    app.Documents.Open(word_file)

    # expression.Execute(FindText, MatchCase, MatchWholeWord,
    #   MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, 
    #   Wrap, Format, ReplaceWith, Replace)
    app.Selection.Find.Execute(find_str, False, False, False, False, False, \
        True, wdFindContinue, False, replace_str, wdReplaceAll)
    app.ActiveDocument.Close(SaveChanges=True)
    app.Quit()

f = 'c:/path/to/my/word.doc'
search_replace_all(f, 'string_to_be_replaced', 'replacement_str')
栩栩如生 2024-08-01 07:18:44

看看是否能让您开始使用 python 进行文字自动化。

打开文档后,您可以执行以下操作。
添加以下代码后,即可关闭文档& 打开另一个。

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "test"
    .Replacement.Text = "test2"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

上面的代码将文本“test”替换为“test2”,并执行“全部替换”。
您可以根据需要将其他选项设置为真/假。

学习这一点的简单方法是创建一个包含您想要执行的操作的宏,请参阅生成的代码和示例。 在您自己的示例中使用它(带/不带修改的参数)。

编辑:在查看 Matthew 的一些代码后,您可以执行以下操作

MSWord.Documents.Open(filename)
Selection = MSWord.Selection

,然后将上面的 VB 代码翻译为 Python。
注意:以下 VB 代码是分配属性的简写方式,不使用长语法。

(VB)

With Selection.Find
    .Text = "test"
    .Replacement.Text = "test2"
End With

Python

find = Selection.Find
find.Text = "test"
find.Replacement.Text = "test2"

请原谅我的 Python 知识。 但是,我希望你有继续前进的想法。
请记住执行“保存并保存”。 完成查找/替换操作后,关闭文档。

最后,您可以调用MSWord.Quit(从内存中释放Word对象)。

See if this gives you a start on word automation using python.

Once you open a document, you could do the following.
After the following code, you can Close the document & open another.

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
    .Text = "test"
    .Replacement.Text = "test2"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

The above code replaces the text "test" with "test2" and does a "replace all".
You can turn other options true/false depending on what you need.

The simple way to learn this is to create a macro with actions you want to take, see the generated code & use it in your own example (with/without modified parameters).

EDIT: After looking at some code by Matthew, you could do the following

MSWord.Documents.Open(filename)
Selection = MSWord.Selection

And then translate the above VB code to Python.
Note: The following VB code is shorthand way of assigning property without using the long syntax.

(VB)

With Selection.Find
    .Text = "test"
    .Replacement.Text = "test2"
End With

Python

find = Selection.Find
find.Text = "test"
find.Replacement.Text = "test2"

Pardon my python knowledge. But, I hope you get the idea to move forward.
Remember to do a Save & Close on Document, after you are done with the find/replace operation.

In the end, you could call MSWord.Quit (to release Word object from memory).

◇流星雨 2024-08-01 07:18:44

如果此邮件列表帖子正确,请访问文档的文本很简单:

MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = 0 
MSWord.Documents.Open(filename)
docText = MSWord.Documents[0].Content

另请参阅如何:搜索和替换文本文档。 这些示例使用 VB 和 C#,但基础知识也应适用于 Python。

If this mailing list post is right, accessing the document's text is a simple as:

MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = 0 
MSWord.Documents.Open(filename)
docText = MSWord.Documents[0].Content

Also see How to: Search for and Replace Text in Documents. The examples use VB and C#, but the basics should apply to Python too.

七色彩虹 2024-08-01 07:18:44

查看此链接:http://python.net/crew/pirx/spam7/

左侧的链接指向文档。

您可以使用对象模型来概括这一点,可以在此处找到该模型:

http://msdn.microsoft.com/en-us/library/kw65a0we(VS.80).aspx

Checkout this link: http://python.net/crew/pirx/spam7/

The links on the left side point to the documentation.

You can generalize this using the object model, which is found here:

http://msdn.microsoft.com/en-us/library/kw65a0we(VS.80).aspx

愛放△進行李 2024-08-01 07:18:44

您还可以使用 VBScript 来实现此目的。 只需将代码键入名为 script.vbs 的文件中,然后打开命令提示符(开始 -> 运行 -> Cmd),然后切换到脚本所在的文件夹并键入:

cscript script.vbs 


strFolder = "C:\Files"

Const wdFormatDocument  = 0

'Select all files in strFolder
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='" & strFolder & "'} Where " _
        & "ResultClass = CIM_DataFile")

'Start MS Word
Set objWord = CreateObject("Word.Application")

Const wdReplaceAll = 2
Const wdOrientLandscape = 1


For Each objFile in colFiles
    If objFile.Extension = "doc" Then
        strFile = strFolder & "\" & objFile.FileName & "." & objFile.Extension
        strNewFile = strFolder & "\" & objFile.FileName & ".doc"
        Wscript.Echo "Processing " & objFile.Name & "..."

        Set objDoc = objWord.Documents.Open(strFile)

        objDoc.PageSetup.Orientation = wdOrientLandscape

        'Replace text - ^p in a string stands for new paragraph; ^m stands for page break
        Set objSelection = objWord.Selection
        objSelection.Find.Text = "String to replace"
        objSelection.Find.Forward = TRUE
        objSelection.Find.Replacement.Text = "New string"

        objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll

        objDoc.SaveAs strNewFile, wdFormatDocument
        objDoc.Close
        Wscript.Echo "Ready"
    End If
Next

objWord.Quit

You can also achieve this using VBScript. Just type the code into a file named script.vbs, then open a command prompt (Start -> Run -> Cmd), then switch to the folder where the script is and type:

cscript script.vbs 


strFolder = "C:\Files"

Const wdFormatDocument  = 0

'Select all files in strFolder
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='" & strFolder & "'} Where " _
        & "ResultClass = CIM_DataFile")

'Start MS Word
Set objWord = CreateObject("Word.Application")

Const wdReplaceAll = 2
Const wdOrientLandscape = 1


For Each objFile in colFiles
    If objFile.Extension = "doc" Then
        strFile = strFolder & "\" & objFile.FileName & "." & objFile.Extension
        strNewFile = strFolder & "\" & objFile.FileName & ".doc"
        Wscript.Echo "Processing " & objFile.Name & "..."

        Set objDoc = objWord.Documents.Open(strFile)

        objDoc.PageSetup.Orientation = wdOrientLandscape

        'Replace text - ^p in a string stands for new paragraph; ^m stands for page break
        Set objSelection = objWord.Selection
        objSelection.Find.Text = "String to replace"
        objSelection.Find.Forward = TRUE
        objSelection.Find.Replacement.Text = "New string"

        objSelection.Find.Execute ,,,,,,,,,,wdReplaceAll

        objDoc.SaveAs strNewFile, wdFormatDocument
        objDoc.Close
        Wscript.Echo "Ready"
    End If
Next

objWord.Quit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文