0

I know similar questions have been asked, but non seem to work for me. I have tried both odt2txt, unoconv and conversion to .csv rather than .txt. I need to find a way (within a bash script) to change an ods file to a txt file, with all columns from ods correctly distributed with tab delimiter, and rows in correct places as well. But every attempt so far has incorrectly converted the file.

Thanks in advance

I would provide an example input and output, but essentially I just want them to look the same, but with input being multiple columns in an ods and output having the same columns but as a tab delimited text file.

There would be some empty rows among some columns (In case that's something that could be causing issues)

  • 1
    I understand you want a scripting solution, but for informational purposes, does the output from LibreOffice "export as TSV" look the way you want? – Wildcard Oct 18 '16 at 22:44
1

Use e.g. unoconv to convert it to CSV, then convert the CSV to tab-separated columns.

Here is a Bash script that uses unoconv and Python for this:

#!/bin/bash
if [ $# -lt 1 -o "$1" = "-h" -o "$1" = "--help" ]; then
    exec >&2
    printf '\n'
    printf 'Usage: %s [ -h | --help ]\n' "$0"
    printf '       %s FILE.ods [ ... ] [ > output.txt ]\n' "$0"
    printf '\n'
    exit 0
fi

unoconv --stdout -f csv "$@" | python -c '
import sys
import csv
from string import maketrans
whitespace = maketrans("\t\r\n", "   ")
def fix(s):
    return str(s).translate(whitespace)
for row in csv.reader(sys.stdin):
    print("\t".join(map(fix, row)))'

This one is paranoid, in that it ensures that the fields do not contain tabs or newlines (they are replaced with a space if they do occur) which would otherwise confuse the output.

If you save the above (temporarily) as ods2tsv, you can install it using

sudo install -o root -g root -m u=rwx,g=rx,o=rx ods2tsv /usr/bin/ods2tsv

and you can use it as e.g.

ods2tsv somefile.ods > your.txt
| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.