]> git.donarmstrong.com Git - bin.git/blobdiff - txt2xls
add latexdiff and txt2xls
[bin.git] / txt2xls
diff --git a/txt2xls b/txt2xls
new file mode 100755 (executable)
index 0000000..97dc8de
--- /dev/null
+++ b/txt2xls
@@ -0,0 +1,146 @@
+#! /usr/bin/perl
+# txt2xls turns text files into excel workbooks, and is released
+# under the terms of the GPL version 2, or any later version, at your
+# option. See the file README and COPYING for more information.
+# Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
+# $Id: perl_script 1153 2008-04-08 00:04:20Z don $
+
+
+use warnings;
+use strict;
+
+use Getopt::Long;
+use Pod::Usage;
+
+=head1 NAME
+
+txt2xls - Turns a (set of) text file(s) into an excel workbook
+
+=head1 SYNOPSIS
+
+ [options]
+
+ Options:
+  --tsv, -t tab separated value mode (Default)
+  --ssv, -s space separated value mode
+  --csv, -c comma separated value mode
+  --r-mode, -r R mode (Default)
+  --debug, -d debugging level (Default 0)
+  --help, -h display this help
+  --man, -m display manual
+
+=head1 OPTIONS
+
+=over
+
+=item B<--debug, -d>
+
+Debug verbosity. (Default 0)
+
+=item B<--help, -h>
+
+Display brief useage information.
+
+=item B<--man, -m>
+
+Display this manual.
+
+=back
+
+=head1 EXAMPLES
+
+
+=cut
+
+
+use vars qw($DEBUG);
+
+use Text::CSV;
+use Spreadsheet::WriteExcel;
+
+my %options = (debug           => 0,
+              help            => 0,
+              man             => 0,
+              rmode           => 1,
+              remove_name     => [],
+              );
+
+GetOptions(\%options,
+          'tsv|t',
+          'ssv|s',
+          'csv|c',
+          'rmode|r-mode|r!',
+          'remove_name|remove-name=s@',
+          'debug|d+','help|h|?','man|m');
+
+pod2usage() if $options{help};
+pod2usage({verbose=>2}) if $options{man};
+
+$DEBUG = $options{debug};
+
+my @USAGE_ERRORS;
+if (0 == grep {exists $options{$_}} qw(tsv ssv csv)) {
+    $options{tsv} = 1
+}
+if (1 < grep {exists $options{$_}} qw(tsv ssv csv)) {
+     push @USAGE_ERRORS,"You can only pass one of --tsv, --ssv, or --csv";
+}
+
+pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
+
+my @columns = ('A'..'Z','AA'..'ZZ');
+
+if (not @ARGV) {
+    # we'll use this as a special indicator to read stdin
+    push @ARGV,undef;
+}
+
+my $sep_char = "\t";
+if ($options{csv}) {
+    $sep_char = ',';
+}
+elsif ($options{ssv}) {
+    $sep_char = ' ';
+}
+
+my $csv = Text::CSV->new({sep_char=>$sep_char});
+my $wb = Spreadsheet::WriteExcel->new(\*STDOUT);
+for my $file (@ARGV) {
+    my $fh;
+    if (not defined $file) {
+       $fh = \*STDIN;
+       $file = "STDIN";
+    }
+    else {
+       $fh = IO::File->new($file,'r') or
+           die "Unable to open $file for reading: $!";
+    }
+    my $ws_name = $file;
+    foreach my $remove (@{$options{remove_name}}) {
+       $ws_name =~ s{\Q$remove\E}{}g;
+    }
+    $ws_name =~ s{\.[^\.]+$}{}g;
+    $ws_name =~ s/_+/ /g;
+    $ws_name =~ s{[\]:*?\/\] ]+}{ }g;
+    $ws_name =~ s{(?:^\s+|\s+$)}{}g;
+    $ws_name =~ s{^(.{0,31}).*$}{$1};
+    my $ws = $wb->add_worksheet($ws_name) or
+       die "Unable to add worksheet to workbook";
+    my $row = 1;
+    while (<$fh>) {
+       chomp;
+       # parse the line
+       my @row;
+       die "Unable to parse line $. of $file" unless $csv->parse($_);
+       if ($row==1) {
+           push @row,'';
+       }
+       push @row,$csv->fields();
+       for my $i (0..$#row) {
+           $ws->write($columns[$i].$row,$row[$i]);
+       }
+       $row++;
+    }
+}
+
+__END__