Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there a commandline tool for viewing/opening excel (.xls) files?

So the answer works great unless the worksheets don't have a custom name.

When I try to open the file, I get:

Traceback (most recent call last):
  File "/usr/bin/py_xls2csv", line 17, in <module>
    for sheet_name, values in parse_xls(arg, 'cp1251'): # parse_xls(arg) -- default encoding
  File "/usr/lib/python2.5/site-packages/pyExcelerator/ImportXLS.py", line 334, in parse_xls
    raise Exception, 'No workbook stream in file.'
Exception: No workbook stream in file.

However, if I open up the file and rename the sheet to 'test' or something it works fine. What do I need to tweak so that it can handle the default names? (Sheet1, etc)

The file I'm trying to open at present has only 1 sheet, named Sheet1.

share|improve this question
add comment

2 Answers 2

up vote 16 down vote accepted

Yeah it's a little bit hacky though. Let's start by installing two packages:

sudo apt-get install python-excelerator w3m

From there, we use a script that comes bundled with python-excelerator to convert the document into a HTML file. We then pipe that into a command line browser (w3m) and display it.

py_xls2html spreadsheet.xls 2>/dev/null | sed 's/"//g' | w3m -dump -T 'text/html'

You can create a bash function or alias with that if you don't want to keep typing it. It should give you output like this:

Sheet = Sheet1
┏━━━━┯━━━┯━━━━━┯━━━━━━━━━━━━┓
┃this│is │a    │spreadsheet ┃
┠────┼───┼─────┼────────────┨
┃it  │is │very │nice        ┃
┠────┼───┼─────┼────────────┨
┃this│has│three│rows        ┃
┗━━━━┷━━━┷━━━━━┷━━━━━━━━━━━━┛
Sheet = Sheet2 Sheet = Sheet3

Very pretteh. Obviously this isn't going to support any sort of macro, editing or any interactivity. This is purely a viewer. You could also work at stripping out the quotation marks that wrap things. I'm not particularly bothered by them at this point.

If you don't need it to be as tabular you could simply have something like this:

py_xls2csv spreadsheet.xls 2>&1 | less

You can go one further than that and display it in a slightly nicer way:

py_xls2csv spreadsheet.xls 2>&1 | grep '^"' | sed 's/"//g' | column -s, -t | less -#2 -N -S

That gives you the following:

  1 this   is    a       spreadsheet
  2 it     is    very    nice
  3 this   has   three   rows
share|improve this answer
    
To dump it to plain text file: links -dump /tmp/tempspreadsheet.html ..... Also: as-is, it prints Unicode characters in \u0950 format... Maybe there is some way to process the output to produce the Unicode character... If someone knows a "simple" way, I'd like to see it... –  Peter.O Jun 8 '11 at 17:47
    
@fred I've switched it to w3m now (because it just looks nicer) and that does have some charset options (see w3m --help). I've also cut the length down, including nuking the need to pipe it out to a file. –  Oli Jun 8 '11 at 17:55
    
Awesome! This works in most cases: see edit to main post for part 2 of the question (It's easier to format stuff there than in a comment) –  David Oneill Jun 8 '11 at 20:10
    
py_xls2csv is hard-coded to work with codepage cp1251.. I've found that substituting the 3 instances of cp1251 with utf-8 resolves the 'problem" of not displaying unicode characters... I suppose it depends on what is in the spreadsheed. but using utf-8 worked for me... (I didin't realize this until after I posted my answer, but it may be of use for other conversions) –  Peter.O Jun 9 '11 at 18:30
    
A belated comment... I just tried using this to tabulate a csv text file (converting to xls first).. and although the generated html has maintained multiple spaces, the rendered output displays them as a single space (typical of html)... ie.. <td> cows         moo </td> displays as cows moo... There is probably some simple tweak for this, but my html knowledge doesn't go far beyond being able to spell 'html' .. :) –  Peter.O Jun 20 '11 at 20:29
add comment

Here is a method which maintains the Unicode characters.
ie. it displays , rather than dislaying the Unicode Codepoint value \U0906

The script uses OpenOffice.org and PyODConverter.py to convert one OOo document format into other OOo format. The conversion types are based on the filename extensions:

  • pdf html odt doc txt ods xls csv odp ppt swf

#
oextn="html" # output type
ifile="My_OOo_File"   ;echo ifile="$ifile"
ofile="$ifile.$oextn" ;echo ofile="$ofile"
[[ -f "$ofile" ]] && { rm "$ofile"; }
[[ -f "$ofile" ]] && { echo "A pre-existing Output file was NOT removed" ;exit; } 
#
sofport=8100
soffice="$(locate -br "^soffice.bin$")"
soffarg=( '-invisible' '-accept=socket,port='$sofport';urp;' )
soffrex="$soffice ${soffarg[@]}"
#
  soffpid=$(($(pgrep -f -n "$soffice")))
((soffpid!=0)) && { echo "A OpenOffice is already running. PID=$soffpid" ;exit; } 
#
# Start OpenOffice
"$soffice" "${soffarg[@]}" &
soffpid=$(($(pgrep -f -n "$soffrex")))
((soffpid== 0)) && { echo "A OpenOffice has not started yet=$soffpid" ;exit; } 
#
# Start OpenOffice
echo "** Attempting to connect to OpenOffice.org on port $sofport"
x=1; while ((x!=0)) ;do
    /usr/bin/python "$(which DocumentConverter.py)" "$ifile" "$ofile"
    x=$?
done
#
# Start Terminal Browser
w3m   "$ofile" -T 'text/html'
kill -15 $soffpid
#

There is probably some other way to exit OOo (but I don't know it)
And there is probably some way of starting an independant instance of OOo too, but I don't know how to do that either, so as it is, it requires that OOo is not running...

share|improve this answer
add comment

protected by Oli Apr 17 '13 at 20:01

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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