Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Maple worksheet (lists) to XLS converter

by mwah (Hermit)
on Oct 10, 2007 at 13:16 UTC ( [id://643950]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info mwah
Description: This script reads a (exported to plain text => .txt)
Maple worksheet (.txt), extracts all 2D number lists
(Arrays [x,y]) of the form
  'list:=[[ ...'
and
  'list := [ ...'
and writes them into an excel worksheet
with column headings named after the lists.

If applied to a unconverted worksheet (.mw)
only input lists are extracted (output lists
will be usually generated by Maple via evaluation
of functions or expressions.

# program "mwlists2xls.pl"
#--------------------------
# Extracts all lists from a maple worksheet (xyz.mw)
#   that *has been converted* to *plain text* before 
#   with: [File] => [Export As] => xyz.txt (plain text)
#
# Usage: perl mwlists2xls.pl xyz.txt
#
# (this will create an excel file: xyz.xls)

use strict;
use warnings;
use Spreadsheet::WriteExcel;
  
my $fn = shift || 'noname.txt';
my $content;

#  - - - READ INPUT MAPLE FILE IN TXT FORMAT- - - - - #
{
 open my $fh,'<', $fn or die "$!";
 local $/; $content = <$fh>
}

# - - EXTRACT input lists of form   'list:=[[' - - - - #
my $rgi = qr { ^
               (
                \w+ := \[ \[ \d [^\n]+
               )
             }xms;
             
my @inplists = $content =~ /$rgi/g;

# - - EXTRACT output lists of form:  list := [0 - - - -#
my $rgo = qr { ^
               \s*
               (
                \w+ \s+ := \s+
                (?: \[[^\]]+ \] [\s,]+ )+
               )
             }xms;
             
my @outlists = $content =~ /$rgo/g;

printf "$fn: found %d input lists, and %d output lists\n",
       scalar@inplists, scalar@outlists;

# - - - CREATE EXCEL workbook - - - - - - - - - - - - #
my $xfn = ($fn=~/^[^\.]+/g)[0] . '.xls';
my $wb = Spreadsheet::WriteExcel->new($xfn);
my $ws = $wb->add_worksheet( substr $xfn, 0, length($xfn)-4 );
my $fmt = $wb->add_format();  $fmt->set_bold();
   $fmt->set_align('center'), $fmt->set_color('orange');

printf "writing into excel workbook %s\n", $xfn;

# - - EXTRACT value/value pairs from individual lists - #
my $rg = qr {
           \[ (\d[^,\s]+) ,\s* ([^\]]+)
          }xs;
          
          
# - - UPDATE COLUMNS IN EXCEL FILE - - - - - - - - - - - #       
my ($col, %lists) = (0, ());

for my $list (@inplists, @outlists) {
   # extract list name from the first field of the list
   my $name = $1 if $list =~ /\s*(\w+)\s*:=/;
   next unless defined $name;
   
   my @data = $list =~ /$rg/g;
   ++$lists{$name} and $name = "${name}_$lists{$name}"; # make name un
+ique
   printf "list $name has %d entries, ", scalar @data;
   
   
   my $row = 0;
   $ws->write($row, $col+0, $name.'_x', $fmt); # X HEADER
   $ws->write($row, $col+1, $name.'_y', $fmt); # Y HEADER
   ++$row;
   
   while( @data ) {
      $ws->write($row, 0+$col, shift @data); # X DATA
      $ws->write($row, 1+$col, shift @data); # Y DATA
      ++$row
   }
   
   print "table column $name created\n";
   $col += 3
}
# - - - - close EXCEL workbook - - - - #
$wb->close();
printf "excel workbook %s closed!\n", $xfn;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-20 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found