我可以使用Win32 COM替换word文档中的文本吗? [英] Can I use Win32 COM to replace text inside a word document?

查看:1251
本文介绍了我可以使用Win32 COM替换word文档中的文本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在一些文档中执行大量的替换,而事情是,我想能够自动执行该任务。一些文档包含通用字符串,如果它可以自动化,这将是非常有用的。从我到目前为止,COM可能是一种方法这样做,但我不知道是否支持文本替换。
我想能够在python中执行这个任务?是否可以?您可以张贴显示如何存取文件文字的程式码片段吗?



谢谢!

解决方案

查看此<



打开文档后,您可以执行以下操作。

在以下代码之后,您可以关闭文档&打开另一个。

  Selection.Find.ClearFormatting 
Selection.Find.Replacement.ClearFormatting
With Selection。查找
.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

结束Selection.Find.Execute替换:= wdReplaceAll

上述代码将文本test替换为test2,并执行全部替换。

您可以转动其他选项true / false取决于你需要什么。



学习这个的简单方法是创建一个宏,包含你想要执行的操作,查看生成的代码&



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

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

然后将上述VB代码转换为Python。

注意:以下VB代码是不使用长语法的属性分配的简写方法。 / p>

(VB)

  With Selection.Find 
。 Text =test
.Replacement.Text =test2

结束

Python

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

放弃我的python知识。但是,我希望你能得到这个想法,继续前进。

记住要做一个Save&在完成查找/替换操作后,关闭Document。



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


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!

解决方案

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).

这篇关于我可以使用Win32 COM替换word文档中的文本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆