如何使用 Perl 中的 Win32::Ole 将格式应用于 docx 文件中的特定单词?

发布于 2024-09-24 09:57:27 字数 1632 浏览 5 评论 0原文

例如,我的 docx 文件包含以下句子:

This is a Perl example
这是一个Python示例
这是另一个 Perl 示例,

我想将粗体样式应用于所有出现的单词“Perl”,如下所示:

这是一个 Perl 示例
这是一个Python示例
这是另一个 Perl 示例,

到目前为止我已经想出了以下脚本:

use strict; use warnings;
use Win32::OLE::Const 'Microsoft Word';

my $file = 'E:\test.docx';

my $Word = Win32::OLE->new('Word.Application', 'Quit');
$Word->{'Visible'} = 0;
my $doc = $Word->Documents->Open($file);
my $paragraphs = $doc->Paragraphs() ;
my $enumerate = new Win32::OLE::Enum($paragraphs);


while(defined(my $paragraph = $enumerate->Next())) {

    my $text = $paragraph->{Range}->{Text};
    my $sel = $Word->Selection;
    my $font = $sel->Font;

    if ($text =~ /Perl/){
        $font->{Bold} = 1;              
    }   
    $sel->TypeText($text);          
}

$Word->ActiveDocument->Close ;
$Word->Quit;

但它对整个段落应用了粗体样式,并且不会在原始位置编辑句子。它给了我修改后的版本和原始版本,如下所示:

这是一个 Perl 示例
这是一个Python示例
这是另一个 Perl 示例
这是一个 Perl 示例
这是一个Python示例
这是另一个 Perl 示例

我应该如何解决我的问题。有什么指点吗?一如既往地感谢:)

更新

问题解决了!非常感谢 @Zaid@cjm :)

这是运行良好的代码:

while ( defined (my $paragraph = $enumerate->Next()) ) {

    my $words = Win32::OLE::Enum->new( $paragraph->{Range}->{Words} );

    while ( defined ( my $word = $words->Next() ) ) {

        my $font = $word->{Font};
        $font->{Bold} = 1 if $word->{Text} =~ /Perl/;
    }
}

For example, my docx file contains the following sentences:

This is a Perl example
This is a Python example
This is another Perl example

I want to apply bold style to all the occurrences of the word "Perl" like so:

This is a Perl example
This is a Python example
This is another Perl example

I've so far come up with the following script:

use strict; use warnings;
use Win32::OLE::Const 'Microsoft Word';

my $file = 'E:\test.docx';

my $Word = Win32::OLE->new('Word.Application', 'Quit');
$Word->{'Visible'} = 0;
my $doc = $Word->Documents->Open($file);
my $paragraphs = $doc->Paragraphs() ;
my $enumerate = new Win32::OLE::Enum($paragraphs);


while(defined(my $paragraph = $enumerate->Next())) {

    my $text = $paragraph->{Range}->{Text};
    my $sel = $Word->Selection;
    my $font = $sel->Font;

    if ($text =~ /Perl/){
        $font->{Bold} = 1;              
    }   
    $sel->TypeText($text);          
}

$Word->ActiveDocument->Close ;
$Word->Quit;

But it has applied bold style to the whole paragraph and it does not edit the sentences in their original place. It gives me both the modified version and the original version like this:

This is a Perl example
This is a Python example
This is another Perl example
This is a Perl example
This is a Python example
This is another Perl example

How should I fix my problem. Any pointers? Thanks like always :)

UPDATE

Problem solved! Big thanks to @Zaid, and @cjm :)

Here's the code that works lovely:

while ( defined (my $paragraph = $enumerate->Next()) ) {

    my $words = Win32::OLE::Enum->new( $paragraph->{Range}->{Words} );

    while ( defined ( my $word = $words->Next() ) ) {

        my $font = $word->{Font};
        $font->{Bold} = 1 if $word->{Text} =~ /Perl/;
    }
}

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

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

发布评论

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

评论(2

攒眉千度 2024-10-01 09:57:27

尝试使用 Words 方法而不是 Text

未经测试:

while ( defined (my $paragraph = $enumerate->Next()) ) {

    my $words = Win32::OLE::Enum->new( $paragraph->{Range}->{Words} );

    while ( defined ( my $word = $words->Next() ) ) {

        my $font = $word->{Font};
        $font->{Bold} = 1 if $word->{Text} =~ /Perl/;
    }
}

Try using the Words method instead of Text.

Untested:

while ( defined (my $paragraph = $enumerate->Next()) ) {

    my $words = Win32::OLE::Enum->new( $paragraph->{Range}->{Words} );

    while ( defined ( my $word = $words->Next() ) ) {

        my $font = $word->{Font};
        $font->{Bold} = 1 if $word->{Text} =~ /Perl/;
    }
}
忆梦 2024-10-01 09:57:27

我对perl一无所知。
但是您查看 Office open xml

您可以使用将 .docx 文件视为 zip 文件,并进行简单的搜索和替换,其工作速度比互操作快一百万倍。而且您也不必担心可能出现的数以百万计的问题。

将您的 .docx 文件重命名为 .zip 并打开它,您就会明白我的意思。

I dont know anything about perl.
But you look at office open xml

You can use treat the .docx file as a zip file, and do a simple search and replace, wich works a million times quicker than the interop. and you dont have to worry about the million things that can go wrong with it also.

Rename your .docx file to .zip and open it and you will see what I mean.

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