ZJ.Mike.2009 has asked for the wisdom of the Perl Monks concerning the following question:
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
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: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;
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
(This question has been cross-posted at stackoverflow.)
Problem solved! thanks to @NetWallah and @sflitmanwith the help from Zaid@stackflow and cjm@stackflow, I've finally solved the problem :) 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/; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How do I apply formatting to a particular word in a docx file using Win32::Ole in Perl?
by NetWallah (Canon) on Sep 23, 2010 at 04:56 UTC | |
by ZJ.Mike.2009 (Scribe) on Sep 23, 2010 at 06:10 UTC | |
|
Re: How do I apply formatting to a particular word in a docx file using Win32::Ole in Perl?
by sflitman (Hermit) on Sep 23, 2010 at 05:06 UTC | |
|
Re: How do I apply formatting to a particular word in a docx file using Win32::Ole in Perl?
by NetWallah (Canon) on Sep 25, 2010 at 16:57 UTC | |
by Anonymous Monk on Jul 26, 2011 at 05:04 UTC | |
by NetWallah (Canon) on Jul 27, 2011 at 04:04 UTC |