-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxlsx2csv.pl
More file actions
executable file
·275 lines (199 loc) · 6.69 KB
/
xlsx2csv.pl
File metadata and controls
executable file
·275 lines (199 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/perl
# xlsx2csv: Convert supported spreadsheet formats to CSV
# (m)'23 [2023-12-04] Copyright H.M.Brand 2008-2024
# Copied from https://github.com/Tux/Spreadsheet-Read/blob/master/scripts/xlsx2csv
require 5.008001;
use strict;
use warnings;
our $VERSION = "3.10";
(my $cmd = $0) =~ s{.*/}{};
use Text::CSV_XS;
use List::Util qw( first );
use Spreadsheet::Read qw( ReadData row ); # libspreadsheet-read-perl libspreadsheet-readsxc-perl
sub usage {
my $err = shift and select STDERR;
print "usage: $cmd [-A [-N | -J c] | -o file.csv] [-s sep] [-f] [-i] file.xls\n";
print " $cmd --help | --man | --info\n";
print " --list List supported spreadsheet formats and exit\n";
print " -A --all Export all sheets (filename-sheetname.csv)\n";
print " -N --no-pfx No filene prefix on -A (sheetname.csv)\n";
print " -Z --zip Convert sheets to CSV's in ZIP\n";
print " -J s --join=s Use s to join filename-sheetname (-)\n";
print " -o f --out=f Set output filename\n";
print " -i f --in=f Set input filename\n";
print " -f --force Force overwrite output if exists\n";
print " -s s --sep=s Set CSV separator character\n";
print "Unless -A is used, all other options are passed on to xlscat\n";
@_ and print join "\n", @_, "";
exit $err;
} # usage
use Getopt::Long qw( :config bundling noignorecase passthrough );
GetOptions (
"help|?" => sub { usage 0; },
"V|version" => sub { print "$cmd [$VERSION]\n"; exit 0; },
"man" => sub { pod_nroff (); },
"info" => sub { pod_text (); },
"list" => sub { list_parsers (); },
"o|c|out=s" => \ my $csv,
"i|x|in=s" => \ my $xls,
"f|force!" => \ my $opt_f,
"s|sep=s" => \ my $opt_s,
"A|all!" => \ my $opt_A,
"N|no-pfx!" => \ my $opt_N,
"Z|zip!" => \ my $opt_Z,
"J|join=s" => \(my $opt_J = "-"),
"v|verbose:1" => \(my $opt_v = 0),
) or usage 1;
sub list_parsers {
print "Ext Parser module Req Has Def\n";
print "----- ----------------------- ----- ----- ---\n";
for (Spreadsheet::Read::parsers ()) {
printf "%-5s %-23s %5s %5s %s\n",
$_->{ext}, $_->{mod}, $_->{min}, $_->{vsn}, $_->{def} ? "<==" : "";
}
exit 0;
} # list_parsers
sub pod_text {
require Pod::Text::Color;
my $m = $ENV{NO_COLOR} ? "Pod::Text" : "Pod::Text::Color";
my $p = $m->new ();
open my $fh, ">", \my $out;
$p->parse_from_file ($0, $fh);
close $fh;
print $out;
exit 0;
} # pod_text
sub pod_nroff {
first { -x "$_/nroff" } grep { -d } split m/:+/ => $ENV{PATH} or pod_text ();
require Pod::Man;
my $p = Pod::Man->new ();
open my $fh, "|-", "nroff", "-man";
$p->parse_from_file ($0, $fh);
close $fh;
exit 0;
} # pod_nroff
unless ($xls) {
foreach my $i (reverse 0 .. $#ARGV) {
-f $ARGV[$i] or next;
$xls = splice @ARGV, $i, 1;
last;
}
}
$xls or usage 1, "No input file";
-r $xls or usage 1, "Input file unreadable";
-s $xls or usage 1, "Input file empty";
my @known_ext = Spreadsheet::Read::parses (0);
@known_ext or @known_ext = qw( csv ods sc sxc xls xlsx );
my $valid_ext = do { local $" = "|"; qr{ \. (?: @known_ext )}xi };
if ($opt_Z) {
require Archive::Zip;
$@ and die "--zip requires Archive::Zip, which could not be loaded\n";
}
my %e = (n => "\n", t => "\t", e => "\e", r => "\r");
$opt_s and $opt_s =~ s/\\+([nter])/$e{$1}/g;
if ($opt_A || $opt_Z) {
$opt_v and warn "Reading $xls ...\n";
my $ss = ReadData ($xls) or die "Cannot read/parse $xls\n";
my $az = $opt_Z ? Archive::Zip->new : undef;
$csv and $xls = $csv;
$xls =~ s/$valid_ext $//ix;
$csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1, eol => "\r\n" });
$opt_s and $csv->sep_char ($opt_s);
foreach my $si (1 .. $ss->[0]{sheets}) {
my $s = $ss->[$si] or next;
$s->{maxcol} or next;
my $mr = $s->{maxrow} or next;
my $sn = $s->{label} || "sheet-$si";
$sn =~ s/\s+$//;
$sn =~ s/^\s+//;
$sn =~ s/[^-\w.]+/_/g; # remove any special chars from worksheet name
if ($opt_Z) {
open my $fh, ">:encoding(utf-8)", \my $data;
$csv->print ($fh, [ row ($s, $_) ]) for 1 .. $mr;
close $fh;
$az->addString ($data, "$sn.csv");
next;
}
my $fn = $opt_N ? "$sn.csv" : "$xls$opt_J$sn.csv";
-f $fn && !$opt_f and die "$fn already exists\n";
$opt_v and warn "Saving sheet to $fn ...\n";
open my $fh, ">:encoding(utf-8)", $fn or die "$fn: $!\n";
$csv->print ($fh, [ row ($s, $_) ]) for 1 .. $mr;
close $fh;
}
if ($opt_Z) {
my $fn = "$xls.zip";
-f $fn && $opt_f and unlink $fn;
-f $fn and die "$fn already exists\n";
$az->writeToFileNamed ($fn) == 0 or die "$fn: $!\n";
}
exit;
}
$csv or ($csv = $xls) =~ s/$valid_ext $/.csv/xi;
if (-f $csv) {
$opt_f or die "$csv already exists\n";
unlink $csv;
}
$opt_v and warn "Converting $xls to $csv ...\n";
open STDOUT, ">", $csv or die "$csv: $!\n";
$^O eq "MSWin32" and $xls = qq{"$xls"};
$opt_s and unshift @ARGV => "--sep=$opt_s";
$opt_v and unshift @ARGV => "--verbose=$opt_v";
exec { "xlscat" } "xlscat", "-c", @ARGV, $xls;
__END__
=pod
=head1 NAME
xlsx2csv - convert spreadsheet to CSV(s)
=head1 SYNOPSIS
xlsx2csv [ -A [ -N || -J str ] | -o file.csv ] [-f] [-i] file.xls
xlsx2csv --help | --man | --info
=head1 DESCRIPTION
Convert a spreadsheet (all formats supported by L<Spreadsheet::Read>) to CSV
(using L<Text::CSV_XS>).
=head1 OPTIONS
=over
=item -?
=item --help
Print short usage and exit.
=item --man
Print this help using nroff and exit.
=item --info
Print this help and exit.
=item -V
=item --version
Print the version and exit.
=item -f
=item --force
Overwrite existing output file.
=item -A
=item --all
Output data from all sheets.
Each sheet will go to extra file with name built from output CSV-file and
sheet name.
=item -N
=item --no-pfx
If specified, dump all will not use output CSV-file but sheet name only.
=item -J str
=item --join=str
If specified, output file names under C<-A> will join on C<str> instead of
the default C<->.
=item -o CSV-file
=item -c CSV-file
=item --out=CSV-file
Output file name (used only with C<< --all >> and without C<< --no-pfx >>).
Default value is derived from XLS-file.
=item -i XLS-file
=item -s XLS-file
=item --in=XLS-file
Allows to specify input xls file.
Default: Last ARGUMENT file that exists.
=back
=head1 SEE ALSO
L<Spreadsheet::Read>, L<Text::CSV_XS>, L<xlscat>
=head1 AUTHOR
H.Merijn Brand <perl5@tux.freedom.nl>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2008-2024 H.Merijn Brand
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut