2

My Python software regularly uses the commands:-

soffice --headless --convert-to ods --outdir /tmp tblIssues.csv
soffice --view /tmp/tblIssues.ods
rm /tmp/tblIssues.ods

given in answer to my question at Open CSV File And Go Straight To Spreadsheet

A User has now requested the ability to view two or more files simultaneously. The problem is the convert will not run again whilst a file is being viewed. No errors are reported so I am somewhat puzzled. If I try and run the convert from the command line it just hangs.

1
  • Don't dispair, I've provided a post that shows some of the ability of LibreOffice that even mighty Excel cannot do.
    – AsymLabs
    Oct 28, 2015 at 15:14

2 Answers 2

1

From the command line or from a script, you can convert (and do other things too) any way you like with LibreOffice.

You can convert and then view multiple files (Example 1) or open a view and convert other files in the background (Example 2) in essentially any fashion that is required.

These actions can be done in serial or in parallel (as coroutines). The key is to create a new user environment and bind this to a tcp port while the conversion or other background process takes place. These can be created at will as long as user locations and ports are available.

The two examples that follow are presented in such a form as to lend themselves to scripting.

An explanation of -env:UserInstallation1 can be found here and the reference for --accept can be found within the table here. The connection can be a socket or a pipe (requiring a named pipe). A summary of the command line options can be found in the LibreOffice documentation. Default settings for LibreOffice are found in bootstraprc1 and sofficerc2.

The syntax may seem a little complex in that uno strings are required. In my view the uno strings are not well documented. But there is an advantage to this approach in that it can be distributed across a network, if required, to enable a document production/processing system.

Example 1. Convert Then View

The following source first illustrates how this is done in parallel (requiring a separate user/port binding for each instance) and how this is done in serial.

# My LibreOffice version is:

# $ libreoffice --version
# LibreOffice 4.2.8.2 420m0(Build:2)

# My files are in a local folder '.'

# $ ls .
# sample00.csv sample01.csv sample02.csv

# Parallel conversion: convert as many as you wish; to be done in parallel
# each instance will exit when completed.  You can set UserInstallation 
# to the location desired, as long as the user can access it, and you can 
# use any tcp port you wish, as long as it is available. The location and
# port are created if they do not exist. Note that each location and port
# must be unique. The location ~/.loports/<port number> is a convenient
# construct. An explanation of -env:UserInstallation and --accept follows.

# -env:UserInstallation=</absolute/path/to/unique/location>
# changes the default user location that is found in bootstraprc.

# --accept
# 'socket,' use a socket 
# 'host=0,' any host (also can use localhost or nnn.nnn.nnn.nnn).
# 'port=NNNN,' can be any available tcp port number.
# 'tcpNoDelay=1;' for uno connections, 0 for 200ms delay (default).

# Now do:

soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8100" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample00.csv &
soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8101" --accept="socket,host=0,port=8101,tcpNoDelay=1;" --convert-to ods --outdir . sample01.csv &
soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8102" --accept="socket,host=0,port=8102,tcpNoDelay=1;" --convert-to ods --outdir . sample02.csv &

# ... and so on ...

# To open multiple views, do:

soffice --nologo --view sample00.ods sample01.ods sample02.ods &

# ... and so on ... 

# Serial conversion:  you can do serial conversion by sending each csv
# file to the same port but you must wait for it to finish first.
# Each conversion will exit when completed. Do something like:

( soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8100" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample00.csv &&
  soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8100" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample01.csv &&
  soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8100" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample02.csv &&
  soffice --nologo --view sample00.ods sample01.ods sample02.ods ) &

# Quick note, when completed, check that your directory, in this case,
# ~/.loports, has been removed. 

Example 2: View Then Convert (In Background)

It is possible to background convert while viewing files. For each instance of LibreOffice, you simply need to create a new user and bind a new tcp port to the user. The following is an example:

# Open a view:

soffice --nologo --view sample00.ods sample01.ods sample02.ods &

# Then convert in the background as you like:

( soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8101" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample00.csv &&
  soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8101" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample01.csv &&
  soffice --headless "-env:UserInstallation=file:///home/$USER/.loports/8101" --accept="socket,host=0,port=8100,tcpNoDelay=1;" --convert-to ods --outdir . sample02.csv ) &

The files are converted in serial; as such they can be bound to the same port. You will note that the files being viewed are also being converted in the background. When converting in parallel, however, each converter must be bound to a unique tcp port. Of course you can use any user/port as long as they are unique and available.

In Closing ...

It is possible to arrange this process as you like, within scripts, without engaging other spreadsheets or software, and to take full advantage of the extensive conversion filters that LibreOffice has to offer.

Optionally, this can be done from within LibreOffice by using ooBasic. There are import export filters that are built in that could be accessed for this purpose, to be actioned by menu options, for example.

As an added bonus, if you do the following after a background conversion (per Example 2), it will force a refresh of LibreOffice, and load the newly converted files:

xdotool search --name sample00.ods windowactivate --sync key --clearmodifiers ctrl+shift+r &&
xdotool search --name sample01.ods windowactivate --sync key --clearmodifiers ctrl+shift+r &&
xdotool search --name sample02.ods windowactivate --sync key --clearmodifiers ctrl+shift+r

But you must install xdotool first, do: sudo apt-get install xdotool.

Footnotes

1 The default UserInstallation setting is found within /usr/lib/libreoffice/program/bootstraprc.

2 The general configuration settings are found at /etc/libreoffice/sofficerc.

6
  • Thank you for this. Where I hit problem was doing a '--headless --convert-to ods' once a file was being viewed. The users are doing a convert/view cycle repeatedly and.then, potentially doing a copy 'n' paste to create a new spreadsheet. Do you know if this cyclic methodology would work as it looks as if you are doing all the converts before viewing? Oct 28, 2015 at 16:45
  • Effectively to do anything in parallel from the cli in LibreOffice you will have to create a new user and new listening port for that user (-env:UserInstallation and --accept). I don't see how it would be possible to view files before they are converted. Is the problem that their files are read only? To avoid that you can eliminate the option --view and they can edit them as they like. Otherwise it should be possible to background convert files and open (or refresh) the viewer when the conversion is completed, in a cyclic manner. This might be better accomplished with a macro.
    – AsymLabs
    Oct 28, 2015 at 17:14
  • @MichaelJohn In addition to background cli conversion (see above) Also have you considered ooBasic? There are many import export filters built in that will background convert file types. This could be added as a menu option or button.
    – AsymLabs
    Oct 28, 2015 at 18:01
  • @MichaelJohn : Updated with added script that illustrates how you can open a view first and then convert files in the background while viewing - no need to close any view while this is being done.
    – AsymLabs
    Oct 28, 2015 at 22:17
  • I will give this a go tomorrow or Monday as a bit tied up on other issues at the moment. Oct 29, 2015 at 16:33
0

After further researching and testing (including using the CLI) it became apparent that once soffice (LibreOffice) had a spreadsheet open it would not start a second instance in –headless mode to convert further .csv files to .ods format. I then found that gnumeric had a conversion program ssconvert. After installing gnumeric I changed the code from:

soffice --headless --convert-to ods --outdir /tmp tblIssues.csv
soffice --view /tmp/tblIssues.ods
rm /tmp/tblIssues.ods

to:-

ssconvert /tmp/tblIssues.csv /tmp/tblIssues.ods
soffice --view /tmp/tblIssues.ods
rm /tmp/tblIssues.csv /tmp/tblIssues.ods

Whilst I am not overly keen on using two separate spreadsheet packages a quick poll suggested the users are not willing to change from LibreOffice. In fact quite a few said “Why can't we have Excel?”

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .