perl - Is there a way to easily take 3 text files and turn it into a multi-tab excel sheet via script? -
is there way take 3 text files , turn multi-tab excel sheet via script?
files file1.txt, file 2.txt, file3.txt - output excelsheet1.xls 3 tabs.
you don't mention format of text files real example code difficult, can use spreadsheet::writeexcel this. @ add_worksheet() method creating new tabs.
given each line number followed text presuming 2 columns per row space delimiting first , second columns , digits in first column. if not true regex below need adjusted. said, here's sample code.
#!/usr/bin/env perl use strict; use warnings; use spreadsheet::writeexcel; sub read_file{ $f = shift; @row; open(my $fh, '<', $f) or die $!; while(<$fh>){ chomp; s/^(d+)s+//; # assuming format of "1 text heren2 more textn" if(defined $1){ push(@row, [$1, $_]); } } close($fh) or die $!; return @row; } if($#argv < 1){ die "$0: file1 [file2 ... filen] output.xls\n"; } $xl = spreadsheet::writeexcel->new(pop); foreach $file (@argv){ if( -f $file){ @rows = read_file($file); $sheet = $xl->add_worksheet($file); $row (0..$#rows){ @cols = @{$rows[$row]}; $col (0..$#cols){ $sheet->write($row, $col, $cols[$col]); } } } }
input files given on command line , processed in order, turning each 1 in tab named after file name. output file name given on command line last, after 1 or more input file names.
edit: including improvements fm mentioned in comment , trivial cli specifying output file name.
Comments
Post a Comment