My Notebook

How to implement part of the Open-Document-Speadsheet file format using only Bash


The Open-Document-Format is an open XML-based standard for spreadsheets, presentations and word processing. In its simplest form it is just a single XML-file in a ZIP-Archive. So there is no need for bloated libraries to create simple documents. All you need is a simple Bash-script and a few common Linux commands.


Some time ago I gathered lots of performance test results in the form of hundreds of separate CSV files. I had to combine all those files into one big spreadsheet for simple data analysis and plotting. Every CSV file should get its own table in the resulting spreadsheet. It is easy to import a CSV file into Open Office, but it is a lot of tedious work to do it for hundreds of files. So I decided to write a script to do it for me.

Content of a minimal ODF file

All ODF files are basically just ZIP files, so you can open them with your favorite archive manager and see what's inside. A typical ODF file contains lots of configuration files, thumbnails and settings, but most of it is unnecessary.

$ unzip -l libreoffice.ods 
Archive:  libreoffice.ods
  Length      Date    Time    Name
---------  ---------- -----   ----
       46  2015-08-15 19:34   mimetype
     3163  2015-08-15 19:34   Thumbnails/thumbnail.png
     7431  2015-08-15 19:34   settings.xml
    14268  2015-08-15 19:34   content.xml
      844  2015-08-15 19:34   meta.xml
    10065  2015-08-15 19:34   styles.xml
        0  2015-08-15 19:34   Configurations2/images/Bitmaps/
        0  2015-08-15 19:34   Configurations2/popupmenu/
        0  2015-08-15 19:34   Configurations2/statusbar/
        0  2015-08-15 19:34   Configurations2/progressbar/
        0  2015-08-15 19:34   Configurations2/toolbar/
        0  2015-08-15 19:34   Configurations2/menubar/
        0  2015-08-15 19:34   Configurations2/floater/
        0  2015-08-15 19:34   Configurations2/accelerator/current.xml
        0  2015-08-15 19:34   Configurations2/toolpanel/
      899  2015-08-15 19:34   manifest.rdf
     1093  2015-08-15 19:34   META-INF/manifest.xml
---------                     -------
    37809                     17 files

Only two of these files are really needed:

$ unzip -l minimal.ods 
Archive:  minimal.ods
  Length      Date    Time    Name
---------  ---------- -----   ----
    14268  2015-08-15 19:34   content.xml
      398  2015-09-01 08:36   META-INF/manifest.xml
---------                     -------
    14666                     2 files
Contains the mime type and a list of files in the archive.
The actual data encoded as XML.

All we need to do is create two XML files and zip them. A Bash-script is ideal for this task, but you can easily do it in any other programming language of your choice.

Creating the Manifest

The file META-INF/manifest.xml contains the mime type of the document and a list with all the files contained in the ZIP archive. The following function takes the mime type and a list of files as parameters and prints the manifest to STDOUT:

function printManifest() {
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"

	echo "<manifest:manifest "
	echo "xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" "
	echo "manifest:version=\"1.2\">"

	# Add mime type
	echo "<manifest:file-entry manifest:full-path=\"/\" manifest:version=\"1.2\""
	echo "manifest:media-type=\"$1\"/>"

	# List all the files in the archive
	for file in "$@"; do
		echo "<manifest:file-entry manifest:full-path=\"$file\""
		echo "manifest:media-type=\"text/xml\"/>"
	echo "</manifest:manifest>"

Encoding the Content

Creating the file content.xml can be divided into the following steps:

Creating the Root-Tag

All namespaces used in the document have to be declared in the root tag of the XML. We only need a few of them for our simple spreadsheet. The function printNamespaces() can be used to conveniently select a subset from the NAMESPACES map.

declare -A NAMESPACES=(

function printNamespaces() {
	for ns in "$@"; do
		echo "xmlns:$ns=\"${NAMESPACES[$ns]}\""

function printHeader() {
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
	echo "<office:document-content office:version=\"1.2\" "

	printNamespaces office table text

	echo "><office:body><office:spreadsheet>"

Parsing of the CSV Input and Table Creation

The function printContent() takes a CSV file as its parameter and prints a XML table to STDOUT. The CSV file is read in line by line. Every line is split into individual cells and every cell then is enclosed in a <table:table-cell> tag.

function printRow() {
    echo "<table:table-row>"

	for cell in $line; do
		# Use bash regex to check if it is a number
		if [[ "$cell" =~ $re ]]; then
			echo "<table:table-cell office:value-type=\"float\""
			echo "office:value=\"$cell\"/>"
			echo "<table:table-cell office:value-type=\"string\">"
			echo "<text:p>$cell</text:p>"
			echo "</table:table-cell>"

	echo "</table:table-row>"

function printContent() {
	# Create a separate table for every input file
	echo "<table:table table:name=\"$(basename $1 .csv)\">"

	# Read the input file line by line
	while IFS='' read -r line || [[ -n "$line" ]]; do
		IFS=';' printRow "$line"
	done < "$1"

	echo "</table:table>"

Closing Open Tags

The following trivial piece of code closes all remaining open tags:

function printFooter() {
    echo "</office:spreadsheet></office:body></office:document-content>"

Everything put together

The following piece of code uses all the functions defined above to create an ODS file from multiple CSV files. It uses a temporary directory to create the directory structure of the ODS file and then adds everything to a ZIP file.


OUTPUT="$(readlink -f $1)"

TMP=`mktemp -d`


printManifest $MIMETYPE $(basename $CONTENT) > $MANIFEST

printHeader > $CONTENT

for file in "$@"; do
    printContent "$file" >> $CONTENT

printFooter >> $CONTENT

rm "$OUTPUT"

cd $TMP
zip "$OUTPUT" *.xml META-INF/*

rm -rf $TMP

The usage of the script is straight forward.

$ sh result.osd *.csv


Although it is really nice to know some of the innards of the ODF standard, creating your own files using Bash scripts is kind of pointless. Most of the time the CSV import functionality of Open Office is all you need. If you need more flexibility, it is probably better to use a proper statistical programming language like R.