Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Using OLE to find MS Word paragraph numbers

by Util (Priest)
on Oct 20, 2011 at 14:13 UTC ( #932669=note: print w/replies, xml ) Need Help??


in reply to Using OLE to find MS Word paragraph numbers

  1. Add my before $paragraph in while(defined($paragraph=$enumerate->Next())).
  2. Typo: Change ListParagraph to ListParagraphs.
  3. Typo: Change ListFormatListLevelNumber to ListFormat->ListLevelNumber.
  4. You might need ListFormat->ListValue instead of (or in addition to) ListFormat->ListLevelNumber.
  5. To match your VB code, check for $count->{Count} == 1 inside the if (defined $count) {...} block.

Working, tested code:

#!/usr/bin/perl use strict; use warnings; use Cwd qw( abs_path ); use Data::Dumper; doc2pt( "C:/a/perls/pm/PM_932635_data.doc" ); sub doc2pt { die if @_ != 1; my ( $doc_path ) = @_; ( my $out_path = $doc_path ) =~ s{\.doc$}{.out} or die "doc_path '$doc_path' does not end in '.doc'"; require Win32::OLE; require Win32::OLE::Enum; # NOTE: Win32::OLE appears to need abs path my $abs_doc_path = abs_path($doc_path); my $document = Win32::OLE->GetObject($abs_doc_path) or die "Can't GetObject($abs_doc_path) $!\n"; print "Extracting Text ...\n"; open my $out_fh, '>', $out_path or die "Can't open output file '$out_path': $!"; my $debug = sub { die if @_ != 2; my ( $name, $value ) = @_; local $Data::Dumper::Useqq = 1; local $Data::Dumper::Terse = 1; printf {$out_fh} "%-10s ==> %s", $name, Dumper $value; }; my $paragraphs = $document->Paragraphs(); my $enumerate = Win32::OLE::Enum->new($paragraphs); while ( my $paragraph = $enumerate->Next() ) { my $style = $paragraph->{Style}->{NameLocal}; $debug->( style => $style ); my $range = $paragraph->{Range}; my $count = $range->ListParagraphs(); if ( defined $count ) { my $real_count = $count->{Count}; my $paranum = $range->ListFormat->ListLevelNumber(); my $paraval = $range->ListFormat->ListValue(); $debug->( real_count => $real_count ); $debug->( paranum => $paranum ); $debug->( paraval => $paraval ); } my $text = $paragraph->{Range}->{Text}; $text =~ tr{\n\r}{}d; $text =~ tr{\x0b}{\n}; $debug->( text => $text ); } close $out_fh; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://932669]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2020-06-01 13:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?