How to combine multiple CSV files to one ods file on the command line. One sheet per original file

How to combine multiple CSV files to one ods file, one sheet per file. I have ten CSV files and I would like to combine them to one ods file comprised of ten sheets, one per original CSV. This should be done on the command line.

2

1 Answer

With perl:

#!/usr/bin/env perl
use strict;
use warnings;
use autodie;
# CPAN modules required:
use Spreadsheet::Write;
use Text::CSV;
my $xlsx_file = shift @ARGV;
$xlsx_file .= ".xlsx" unless $xlsx_file =~ /\.xlsx$/;
my $xlsx = Spreadsheet::Write->new(file => $xlsx_file);
my $csv = Text::CSV->new({binary => 1});
for my $csv_file (@ARGV) { my @rows = (); open my $fh, "<:encoding(utf8)", $csv_file; while (my $row = $csv->getline($fh)) { push @rows, $row; } $csv->eof or $csv->error_diag(); close $fh; (my $sheet_name = $csv_file) =~ s/\.[^.]+$//; # strip extension $xlsx->addsheet($sheet_name); $xlsx->addrows(@rows);
}
$xlsx->close();

And use it like:

/path/to/create_xlsx.pl file.xlsx *.csv

If perl is not your thing, a bit of googling reveals:

  • python:
  • ruby:
4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like