Results 1 to 2 of 2
Hi gurus, i am trying to write simple perl script using win32 ole which will iterate over all M$ word paragraphs (any text that ends with a hard return) and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 08-22-2012 #1Just Joined!
- Join Date
- Jul 2009
- Posts
- 70
Perl OLE Selection property iteration in cycle
Hi gurus, i am trying to write simple perl script using win32 ole which will iterate over all M$ word paragraphs (any text that ends with a hard return) and print only those paragraphs that matches the specified condition. The problem is that I need to access font size property. It seems to me that this property is set only once in first paragraph and later is not updated. Please see following code:
Here is output what I obtained:Code:#!/usr/bin/perl use strict; use warnings; use Win32::OLE::Const 'Microsoft Word'; #$Win32::OLE::CP = CP_UTF8; binmode STDOUT, 'encoding(utf8)'; # OPEN FILE SPECIFIED AS FIRST ARGUMENT my $fname=$ARGV[0]; my $fnameFullPath = `cygpath.exe -wa $fname`; $fnameFullPath =~ s/\\/\\\\/g; $fnameFullPath =~ s/\s*$//; unless (-e $fnameFullPath) { print "Error: File did not exists\n"; exit 1;} # STARTING OLE my $Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application','Quit') or die Win32::OLE->LastError(); $Word->{'Visible'} = 0; my $doc = $Word->Documents->Open($fnameFullPath); my $paragraphs = $doc->Paragraphs() ; my $enumerate = new Win32::OLE::Enum($paragraphs); # PROCESSING PARAGRAPHS while(defined(my $paragraph = $enumerate->Next())) { my $text = $paragraph->{Range}->{Text}; my $sel = $Word->Selection; my $font = $sel->Font; if ($font->{Size} == 18){ print "Text: ", $text, "\n"; print "Font Bold: ", $font->{Bold}, "\n"; print "Font Italic: ", $font->{Italic}, "\n"; print "Font Name: ", $font->{Name}, "\n"; print "Font Size: ", $font->{Size}, "\n"; print "=========\n"; } } # CLOSING OLE $Word->ActiveDocument->Close ; $Word->Quit;
As you can see in output everywhere is Font Size 18 even if in original document are different sizes (Also font name is not updated). This brings me to assumption that $font is set only once in 1st paragraph which is processed. Thus the following conditionCode:Text: This is a doc file containing different fonts and size, document also contain header and footer (Font: TNR, Size: 18) Font Bold: 0 Font Italic: 0 Font Name: Times New Roman Font Size: 18 ========= Text: This is a Perl example (Font TNR, Size: 12) Font Bold: 0 Font Italic: 0 Font Name: Times New Roman Font Size: 18 ========= Text: This is a Python example(Font: Courier New, Size: 10) Font Bold: 0 Font Italic: 0 Font Name: Times New Roman Font Size: 18 =========
is only evaluated in 1st processed paragraph. This also supports fact that if I change condition to following (Match 2nd paragraph):Code:if ($font->{Size} == 18)
the output is nothing. Because first paragraph is 18 not 12 and thus the condition is false, $font is not updated any more so it wont never be true. What I am doing wrong ?Code:if ($font->{Size} == 12)
Many thanks
- 08-23-2012 #2Trusted Penguin
- Join Date
- May 2011
- Posts
- 3,745
what happens if you just leave off the font-size checking logic totally? e.g.:
Code:# if ($font->{Size} == 18){ print "Text: ", $text, "\n"; print "Font Bold: ", $font->{Bold}, "\n"; print "Font Italic: ", $font->{Italic}, "\n"; print "Font Name: ", $font->{Name}, "\n"; print "Font Size: ", $font->{Size}, "\n"; print "=========\n"; # }


Reply With Quote
