我有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";
}
输入文字文件:
输出:与文本文件相同 "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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句