Debian Codegraph @

examples/text2table

raw
1 #!/usr/bin/perl
2 #-----------------------------------------------------------------------------
3 # $Id : text2table 0.2 2008-05-13 JMG$
4 #-----------------------------------------------------------------------------
5
6 =head1 NAME
7
8 text2table - flat text conversion to OpenDocument spreadsheet
9
10 =head1 SYNOPSIS
11
12 text2table sourcefile.csv mycalc.ods 32 20
13
14 =head1 DESCRIPTION
15
16 Creates an ODF spreadsheet file and populate its first sheet
17 from a delimited text file. Each line of the source file produces
18 a row in the target table. In the line, the field separator is ";".
19
20 The target file is created. Any existing file with the same name is
21 replaced.
22
23 The two last arguments are the length (lines) and the width (columns)
24 of the target table.
25
26 The input values are processed as text values.
27
28 =cut
29
30 use OpenOffice::OODoc 2.103;
31
32 my ($input_file, $output_file, $lines, $columns) = @ARGV;
33
34 die "Usage : txt2table <input file> <output OOo file> <lines> <columns>\n"
35 unless ($input_file && $output_file && $lines && $columns);
36
37 # create the OOo spreadsheet
38 my $doc = odfDocument(file => $output_file, create => 'spreadsheet');
39
40 # select & size the 1st (and only) sheet in the document
41 my $sheet = $doc->expandTable(0, $lines, $columns);
42 # rename it as the input file (why not ?)
43 $doc->renameTable($sheet, $input_file);
44
45 # populate the table
46 open INPUT, "<", $input_file or die "Input file $input_file unavailable\n";
47 my $line = undef;
48 my @rows = $doc->getTableRows($sheet);
49 for (my $i = 0 ; $i < $lines && ($line = <INPUT>) ; $i++)
50 {
51 my $row = $rows[$i];
52 my @cellvalues = split ';', $line;
53 my @cells = $doc->getRowCells($row);
54 for (my $j = 0 ; $j < $columns && @cellvalues ; $j++)
55 {
56 $doc->cellValue($cells[$j], shift @cellvalues);
57 }
58 }
59
60 # save the result
61 $doc->save;
62 exit;
63