]> git.donarmstrong.com Git - bin.git/blob - txt2xls
* add tex-only optino
[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   --debug, -d debugging level (Default 0)
29   --help, -h display this help
30   --man, -m display manual
31
32 =head1 OPTIONS
33
34 =over
35
36 =item B<--debug, -d>
37
38 Debug verbosity. (Default 0)
39
40 =item B<--help, -h>
41
42 Display brief useage information.
43
44 =item B<--man, -m>
45
46 Display this manual.
47
48 =back
49
50 =head1 EXAMPLES
51
52
53 =cut
54
55
56 use vars qw($DEBUG);
57
58 use Text::CSV;
59 use Spreadsheet::WriteExcel;
60
61 my %options = (debug           => 0,
62                help            => 0,
63                man             => 0,
64                rmode           => 1,
65                remove_name     => [],
66                );
67
68 GetOptions(\%options,
69            'tsv|t',
70            'ssv|s',
71            'csv|c',
72            'rmode|r-mode|r!',
73            'remove_name|remove-name=s@',
74            'debug|d+','help|h|?','man|m');
75
76 pod2usage() if $options{help};
77 pod2usage({verbose=>2}) if $options{man};
78
79 $DEBUG = $options{debug};
80
81 my @USAGE_ERRORS;
82 if (0 == grep {exists $options{$_}} qw(tsv ssv csv)) {
83     $options{tsv} = 1
84 }
85 if (1 < grep {exists $options{$_}} qw(tsv ssv csv)) {
86      push @USAGE_ERRORS,"You can only pass one of --tsv, --ssv, or --csv";
87 }
88
89 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
90
91 my @columns = ('A'..'Z','AA'..'ZZ');
92
93 if (not @ARGV) {
94     # we'll use this as a special indicator to read stdin
95     push @ARGV,undef;
96 }
97
98 my $sep_char = "\t";
99 if ($options{csv}) {
100     $sep_char = ',';
101 }
102 elsif ($options{ssv}) {
103     $sep_char = ' ';
104 }
105
106 my $csv = Text::CSV->new({sep_char=>$sep_char});
107 my $wb = Spreadsheet::WriteExcel->new(\*STDOUT);
108 for my $file (@ARGV) {
109     my $fh;
110     if (not defined $file) {
111         $fh = \*STDIN;
112         $file = "STDIN";
113     }
114     else {
115         $fh = IO::File->new($file,'r') or
116             die "Unable to open $file for reading: $!";
117     }
118     my $ws_name = $file;
119     foreach my $remove (@{$options{remove_name}}) {
120         $ws_name =~ s{\Q$remove\E}{}g;
121     }
122     $ws_name =~ s{\.[^\.]+$}{}g;
123     $ws_name =~ s/_+/ /g;
124     $ws_name =~ s{[\]:*?\/\] ]+}{ }g;
125     $ws_name =~ s{(?:^\s+|\s+$)}{}g;
126     $ws_name =~ s{^(.{0,31}).*$}{$1};
127     my $ws = $wb->add_worksheet($ws_name) or
128         die "Unable to add worksheet to workbook";
129     my $row = 1;
130     while (<$fh>) {
131         chomp;
132         # parse the line
133         my @row;
134         die "Unable to parse line $. of $file" unless $csv->parse($_);
135         if ($row==1) {
136             push @row,'';
137         }
138         push @row,$csv->fields();
139         for my $i (0..$#row) {
140             $ws->write($columns[$i].$row,$row[$i]);
141         }
142         $row++;
143     }
144 }
145
146 __END__