【问题标题】:Find and replace a word in DOC document Perl在 DOC 文档 Perl 中查找和替换单词
【发布时间】:2011-08-13 15:42:41
【问题描述】:

我有一堆 Microsoft Word 文档,并且想要查找和替换一个单词,例如

找到banana这个词替换成apple

【问题讨论】:

标签: perl


【解决方案1】:

你可以使用Win32::OLE

看起来你的交互看起来像这样(完全未经测试):

use Win32::OLE;

my $word = Win32::OLE->new('Word.Application') or die "Could not find Word.\n";

my $doc = $word->Documents->Open($filename);
my $content = valof $doc->{Content};
$content =~ s/banana/apple/g;
$doc->{Content} = $content;
$doc->Save();
$doc->Close();
$word->Quit();

阅读 Win32::OLE 文档以及 VB 文档以获得更好/更多的信息。

您可能还会发现this page 底部的“Multireplace”工具很有趣。

【讨论】:

  • 您不能在 Range 对象上进行正则表达式替换。
  • @Zaid:感谢您提供的信息。我现在编辑为使用valof,但我无法对此进行测试。也许你有更好的答案?
  • 我链接的related question中有一个合理的例子。
  • 还可以通过 Win32::OLE 编写 Word 的查找和替换功能。我已经忘记了它实际上是如何暴露的,因为我上次使用它是在大约 10 年前,但这是可能的。
  • @Svante:Getting Error Can't call method "valof" on an undefined value at word_find_replace.pl line 10.
猜你喜欢
  • 1970-01-01
  • 2011-04-25
  • 2014-04-08
  • 1970-01-01
  • 2012-01-04
  • 2014-07-26
  • 2010-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多