]> git.donarmstrong.com Git - bin.git/blob - txt2xls
remove convert_to_xls and .sa_bin
[bin.git] / txt2xls
1 #! /usr/bin/perl
2 # txt2xls turns text files into excel workbooks, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 1153 2008-04-08 00:04:20Z don $
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 txt2xls - Turns a (set of) text file(s) into an excel workbook
18
19 =head1 SYNOPSIS
20
21  [options]
22
23  Options:
24   --tsv, -t tab separated value mode (Default)
25   --ssv, -s space separated value mode
26   --csv, -c comma separated value mode
27   --r-mode, -r R mode (Default)
28   --auto-format Auto format (Default)
29   --sci-format Scientific format formatting string (Default 0.00E+0)
30   --max-digits Maximum digits to use for auto-format (Default 4)
31   --debug, -d debugging level (Default 0)
32   --help, -h display this help
33   --man, -m display manual
34
35 =head1 OPTIONS
36
37 =over
38
39 =item B<--auto-format>
40
41 Attempt to automatically format the excel file. Currently, this does
42 nothing for non-numeric entries. For numeric entries, if the number is
43 very large (> 9999), or less than 0.001, but not 0, the cell is put
44 into scientific format (B<--sci-format>, default C<0.00E+0>).
45 Otherwise, the cell is formatted to use at maximum B<--max-digits>
46 (default 4) digits.
47
48 To disable, use B<--no-auto-format>
49
50 =item B<--sci-format>
51
52 Excel format string to use for scientific format. See
53 L<http://office.microsoft.com/en-us/excel-help/number-format-codes-HP005198679.aspx>
54 for details. (Default C<'0.00E+0'>)
55
56 =item B<--max-digits>
57
58 Maximum number digits to display for non-scientific formats. (Default 4)
59
60 =item B<--debug, -d>
61
62 Debug verbosity. (Default 0)
63
64 =item B<--help, -h>
65
66 Display brief useage information.
67
68 =item B<--man, -m>
69
70 Display this manual.
71
72 =back
73
74 =head1 EXAMPLES
75
76
77 =cut
78
79
80 use vars qw($DEBUG);
81
82 use Text::CSV;
83 use Spreadsheet::WriteExcel;
84 use Scalar::Util qw(looks_like_number);
85 use POSIX qw(floor);
86
87 my %options = (debug           => 0,
88                help            => 0,
89                man             => 0,
90                auto_format     => 1,
91                sci_format      => '0.00E+0',
92                max_digits      => 4,
93                remove_name     => [],
94                );
95
96 GetOptions(\%options,
97            'tsv|t',
98            'ssv|s',
99            'csv|c',
100            'auto_format|auto-format!',
101            'sci_format|sci-format=s',
102            'max_digits|max-digits=i',
103            'rmode|r-mode|r!',
104            'remove_name|remove-name=s@',
105            'debug|d+','help|h|?','man|m');
106
107 pod2usage() if $options{help};
108 pod2usage({verbose=>2}) if $options{man};
109
110 $DEBUG = $options{debug};
111
112 my @USAGE_ERRORS;
113 if (0 == grep {exists $options{$_}} qw(tsv ssv csv)) {
114     $options{tsv} = 1
115 }
116 if (1 < grep {exists $options{$_}} qw(tsv ssv csv)) {
117      push @USAGE_ERRORS,"You can only pass one of --tsv, --ssv, or --csv";
118 }
119
120 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
121
122 if (not @ARGV) {
123     # we'll use this as a special indicator to read stdin
124     push @ARGV,undef;
125 }
126
127 my $sep_char = "\t";
128 if ($options{csv}) {
129     $sep_char = ',';
130 }
131 elsif ($options{ssv}) {
132     $sep_char = ' ';
133 }
134
135 if (not @{$options{remove_name}}) {
136     $options{remove_name} = ['.+\/',
137                             ];
138 }
139
140 my %wb_formats = ();
141 my $csv = Text::CSV->new({sep_char=>$sep_char});
142 my $wb = Spreadsheet::WriteExcel->new(\*STDOUT);
143 for my $file (@ARGV) {
144     my $fh;
145     if (not defined $file) {
146         $fh = \*STDIN;
147         $file = "STDIN";
148     }
149     else {
150         open($fh,'<:encoding(utf8)',$file) or
151             die "Unable to open $file for reading: $!";
152     }
153     my $ws_name = $file;
154     foreach my $remove (@{$options{remove_name}}) {
155         $ws_name =~ s{$remove}{}g;
156     }
157     $ws_name =~ s{\.[^\.]+$}{}g;
158     $ws_name =~ s/_+/ /g;
159     $ws_name =~ s{[\]:*?\/\] ]+}{ }g;
160     $ws_name =~ s{(?:^\s+|\s+$)}{}g;
161     $ws_name =~ s{^(.{0,31}).*$}{$1};
162     my $ws = $wb->add_worksheet($ws_name) or
163         die "Unable to add worksheet to workbook";
164     my $row = 1;
165     my @header_row;
166     my $overflow = 0;
167     my $r_mode = $options{r_mode} // 0;
168     # set to 1 if we have attempted to autodetect R mode
169     my $r_mode_autodetected = 0;
170     while (<$fh>) {
171         chomp;
172         # parse the line
173         die "Unable to parse line $. of $file: ".$csv->error_diag() unless $csv->parse($_);
174         my @row = $csv->fields();
175         if ($row==1 and not $r_mode_autodetected) {
176             @header_row = @row;
177             $row++;
178             next;
179         }
180         if ($row==2 and not $r_mode_autodetected) {
181             $r_mode_autodetected = 1;
182             if (@row == (@header_row+1)) {
183                 $r_mode = 1 unless exists $options{r_mode} and defined $options{r_mode};
184             }
185             if ($r_mode) {
186                 # R doesn't output headers for rownames
187                 unshift @header_row,'';
188             }
189             output_row(\@header_row,1,$ws,$wb,\%wb_formats,\%options);
190         }
191         if ($row > 65536) { # ok, we're going to overflow here
192             my $t_ws_name = $ws_name;
193             my $maxlen = 31-length('.'.$overflow);
194             $t_ws_name =~ s{^(.{0,$maxlen}).*$}{$1};
195             $ws = $wb->add_worksheet($t_ws_name.'.'.$overflow);
196             $overflow++;
197             $row=1;
198             output_row(\@header_row,$row,$ws,$wb,\%wb_formats,\%options);
199             $row++;
200         }
201         if ($row==1) {
202             @header_row = @row;
203         }
204         output_row(\@row,$row,$ws,$wb,\%wb_formats,\%options);
205         $row++;
206     }
207 }
208
209 sub output_row{
210     my ($data,$row,$ws,$wb,$formats,$options) = @_;
211     my @columns = ('A'..'Z','AA'..'ZZ');
212     for my $i (0..$#{$data}) {
213         my $format;
214         if ($options->{auto_format}) {
215             if (looks_like_number($data->[$i])) {
216                 my $format_string;
217                 # use scientific format?
218                 if ($data->[$i] != 0 && (abs($data->[$i]) > 9999 ||
219                                          abs($data->[$i]) < 0.001)) {
220                     $format_string = $options->{sci_format};
221                 } else {
222                     $format_string = '0';
223                     my $digits = length(floor(abs($data->[$i])));
224                     if ($options->{max_digits} - $digits > 0 and
225                         abs($data->[$i]) != abs(floor($data->[$i]))) {
226                         # if there are digits left over, use them for
227                         # decimal places, but don't require them
228                         $format_string = $format_string.'.'.('#'x($options->{max_digits} - $digits))
229                     }
230                 }
231                 if (not exists $formats->{$format_string}) {
232                     $formats->{$format_string} =
233                         $wb->add_format();
234                     $formats->{$format_string}->
235                         set_num_format($format_string);
236                 }
237                 $format = $formats->{$format_string};
238             }
239         }
240         $ws->write($columns[$i].$row,$data->[$i],defined $format?$format:())
241     }
242 }
243
244 __END__