csv2odf - convert csv files to open document format
csv2odf [option] [<csvfile>] <templatefile> [<outputfile>]
csv2odf is a command line tool that can convert a comma seperated value (csv) file into an ods, odt, html, xlsx, or docx document. csv2odf is useful for creating reports from databases and other data sources that produce csv files. csv2odf can be combined with cron and shell scripts to automatically generate business reports.
The csv data is merged with a template file to produce the output file. The template is a document file as produced by OpenOffice.org or Word, or an html file. The template can be a spreadsheet file (ods or xlsx), a document file (odt or docx), or an html file.
If a csv input file is not specified, input will be taken from standard input (stdin). If an output file is not specified, output will be directed to standard output (stdout). This allows the use of the command in pipes.
The first row of the table will be treated as a header row. (Override with -H) The formatting in the second row of cells will be applied to each data cell of the output file.
If the template is a spreadsheet, the second row should contain some data of similar type to the incoming data so that numbers/text/dates can be correctly identified.
If the template is a document, it must contain a table that the csv data will be inserted into. If the first table in the file is not the target, use the -t options to identify the table number.
If the template is an html file, it must contain a table that the csv data will be inserted into. If the first table in the file is not the target, use the -t options to identify the table number or table id property. The table may be made using div tags if the --div options is used, in which case a div tag must enclose each cell, each row, and the entire table.
In the header or footer of the template, you can insert a [date] tag (the word "date" enclosed in brackets). The date tag will be replaced by the current date. The date format can be changed using format codes (see below), for example [date %Y-%m-%d] will produce a date like 2008-02-04. A date with a day offset from the current date can be created by using +n or -n, for example [date-1] would insert the date before today.
The options may be placed in the template file. To do this, put "csv2odf:" (without quotes) followed by the options in the first cell (cell A1). Note the first row will be deleted when it contains options.
The date format codes are the same ones used by Python’s strftime function.
%a Locale’s abbreviated weekday name.
%A Locale’s full weekday name.
%b Locale’s abbreviated month name.
%B Locale’s full month name.
%c Locale’s appropriate date and time representation.
%d Day of the month as a decimal number [01-31].
%H Hour (24-hour clock) as a decimal number [00-23].
%I Hour (12-hour clock) as a decimal number [01-12].
%j Day of the year as a decimal number [001-366].
%m Month as a decimal number [01-12].
%M Minute as a decimal number [00-59].
%p Locale’s equivalent of either AM or PM.
%S Second as a decimal number [00-61].
%U Week number of the year (Sunday as the first day of the week)
as a decimal number [00-53]. All days in a new year preceding
the first Sunday are considered to be in week 0
%w Weekday as a decimal number [0(Sunday)-6].
%W Week number of the year (Monday as the first day of the week)
as a decimal number [00-53]. All days in a new year preceding
the first Monday are considered to be in week 0.
%x Locale’s appropriate date representation.
%X Locale’s appropriate time representation.
%y Year without century as a decimal number [00-99].
%Y Year with century as a decimal number.
%Z Time zone name (no characters if no time zone exists).
%% A literal "%" character.
Insert data into a spreadsheet:
Insert data into an OpenOffice text document:
Specify format for dates in the csv file:
Dates in the csv file can be almost any format. You can tell the program the format to be expected with the -d option. In ods files, the program knows which columns are dates by looking for date formatted cells in the template. In xlsx files, format the date then mark the cell with the text "csv2odf-date" (without quotes).
The csv file has a header on the first row:
When the csv file has column names on the first row, -H will cause the column name to be put into the header of the output file.
The csv file has a header but we don’t want to use it:
This will use data from the csv file beginning with the second row.