This command line utility and its reusable Python module convert CSV files into HTML tables or complete HTML documents. They can use either the old school HTMLgen
module or the more modern html
module (both for Python 2.x). The later (available from https://pypi.python.org/pypi/html or through pip install html
) is preferred since it can be used to generate valid HTML 4.01 Strict output. By default csv2html takes the first row of the CSV file as the header of the HTML table.
Note: for the sake of compatibility with the current defaults of LibreOffice Calc csv2html assumes the field delimiter to be ;
unless you specify otherwise; use the option -d ,
for most files generated by Microsoft Excel and others.
usage: csv2html.py [-h] [-o output] [-t TITLE] [-d DELIM] [-s N] [-r] [-n]
[-c] [-g]
input
Converts CSV files into HTML tables
positional arguments:
input input file
optional arguments:
-h, --help show this help message and exit
-o output, --output output
output file
-t TITLE, --title TITLE
document & table title
-d DELIM, --delimiter DELIM
field delimiter for CSV (";" by default)
-s N, --start N skip the first N-1 rows, start with row N
-r, --renumber replace the first column with row numbers
-n, --no-header do not use the first row of the input as the header
-c, --complete-document
output a complete HTML document instead of only the
table
-g, --force-htmlgen uses HTMLgen even if the html module is available
This takes comma-delimited data from AssetsImportCompleteSample.csv and outputs the corresonding HTML table to test.html:
./csv2html.py -o test.html -d , AssetsImportCompleteSample.csv
The example below takes the data from pub.csv, starting with row 267. The first column of the table is replaced with row numbers starting at 1 (except in the header row, which remains untouched in the output). The output is redirected to the file pub.html.
./csv2html.py pub.csv -r -s 267 > pub.html
Same as above but this time (due to the option -c
) the output is a full HTML document instead of just the markup for the table. The option -g
forces csv2html to produce its HTML output using HTMLgen. The output is then fed to HTML Tidy (which helps with HTMLgen's upper-case tags) and saved to pub.html.
./csv2html.py pub.csv -r -s 267 -c -g | tidy -q > pub.html