#!/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; }