本文介绍了如何在Perl中使用Win32 :: Ole将格式应用于docx文件中的特定单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是一个Perl示例

这是一个Python示例

这是另一个Perl示例



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



这是一个 Perl 示例

这是一个Python示例

这是另一个 Perl 示例



我已经拿出下面的脚本:

pre $ 严格使用;使用警告;
使用Win32 :: OLE :: Const'Microsoft Word';

my $ file ='E:\test.docx';
$ b $ my $ Word = Win32 :: OLE-> new('Word.Application','Quit');
$ Word-> {'Visible'} = 0;
my $ doc = $ Word-> Documents-> Open($ file);
my $ paragraph = $ doc->段落();
my $ enumerate = new Win32 :: OLE :: Enum($ paragraph);


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

my $ text = $ paragraph-> {Range } - GT; {文字};
my $ sel = $ Word->选择;
my $ font = $ sel->字体;

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

$ Word-> ActiveDocument->关闭;
$ Word->退出;

但是它在整个段落中应用了大胆的风格,并且不会在原始位置编辑句子。它给了我两个修改版本和原始版本,如下所示:

这是一个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 /;



解决方案

使用 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 /;
}
}


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/;
    }
}
解决方案

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/;
    }
}

这篇关于如何在Perl中使用Win32 :: Ole将格式应用于docx文件中的特定单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 15:53