Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Win32::OLE reading word file with tables

by chafelix (Acolyte)
on Oct 04, 2025 at 19:12 UTC ( [id://11166420]=perlquestion: print w/replies, xml ) Need Help??

chafelix has asked for the wisdom of the Perl Monks concerning the following question:

I am reading a word file which has tables, using Win32::OLE. I am only interested in the tables. The code I use is shown below
use Win32::OLE qw(in); use Win32::OLE::Const 'Microsoft Word'; sub print_tables { my $word =Win32::OLE->new('Word.Application','Quit'); # shift; my $word_file = file(abs_path(shift)); my $doc = $word->{Documents}->Open("$word_file"); my $tables = $word->ActiveDocument->{Tables}; for my $table (in $tables) { my $numrows=$table->Rows->Count; my $rownum=1; while($rownum<=$table->Rows->Count){ ... $main::numcols=$table->Columns->Count; my $cell5=$table->Cell($rownum,$main::numcols);#this is the last colum +n ..... } } }
The problem arises when the tables have merged columns, so that the number of columns is < $main::numcols In that case I need to check and essantially say either "if $actual_number_of_columns_in_present_row <$main::numcols){...}" or catch the exception "(If ! $cell5 ){...}" So the question is how do I say either of these?

Replies are listed 'Best First'.
Re: Win32::OLE reading word file with tables
by Marshall (Canon) on Oct 06, 2025 at 07:07 UTC
    You can query the number of columns on a particular row. This would be more general and allow for say 2 or 3 columns to be merged together. Also, I think this foreach type of loop will be faster than your while because the fancy object method $table->Rows->Count is only evaluated once and not on every loop iteration.

    # Iterate through rows and count columns foreach my $row_index (1 .. $table->Rows->Count) { my $row = $table->Rows->Item($row_index); my $column_count = $row->Cells->Count; print "Row $row_index has $column_count columns.\n"; }
    posting quickly because the website is unstable and I am getting timeouts when loading pages

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11166420]
Approved by johngg
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2025-11-12 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (68 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.