使用 perl 脚本获取 ms-word 文件中的所有正常样式

Fetch all normal style in ms-word file using perl script

我有 Ms-Word 文档文件(也可能是 *DOCX)我需要使用 perl 脚本获取文档中使用的所有样式。

我尝试了搜索引擎中的一些代码,但我失败了。

use Win32::OLE;
use Win32::OLE::Enum;
use File::Copy;
#use strict;

my $fileName = "d:\test.doc";
my $document = Win32::OLE -> GetObject($fileName);
#Creating a new excel sheet
#my $xl_app=Win32::OLE->new('word.Application','Quit');

my $paragraphs = $document->Paragraphs();
my $enumerate = new Win32::OLE::Enum($paragraphs);

while(defined($enumerate->Next()))
{
    $style = $paragraph->{'Styles'}->{'Normal'};
    $text = $paragraph->{'Range'}->{'Text'};
    #$text =~ s/[\n\r]//g;
    #$text =~ s/\x0b/\n/g;
    #$text =~ s/\x07//g;
    print "\nStyle = $style";
    print "\nText = $text";
}

输入word文档:

输出:与文本文件相同"Normal style found: Line number nn"

有人可以帮我解决这个问题吗?提前致谢。

以下对我有用:

use feature qw(say)
use strict;
use warnings;
use Win32::OLE;
use Win32::OLE::Enum;
use constant wdFirstCharacterLineNumber => 10;
my $fn = "d:\test.docx";
my $document = Win32::OLE->GetObject($fn) or die Win32::OLE->LastError;
my $word_app = Win32::OLE->GetActiveObject('Word.Application') or die Win32::OLE->LastError;
my $paragraphs = $document->Paragraphs();
my $num_para = $document->Paragraphs->Count;
for my $i (1..$num_para) {
    my $paragraph = $paragraphs->Item($i);
    my $style = $paragraph->{Style}->{NameLocal};
    if ($style eq "Normal") {
        my $range = $paragraph->Range;
        $range->Select;
        my $line_no = $word_app->Selection->Information(wdFirstCharacterLineNumber);
        say "Normal style found: Line number $line_no";
    }
}
$document->close();