LibreOffice and Apache OpenOffice at the command line

By the Script

Article from Issue 157/2013
Author(s):

Your favorite office suites have a surprisingly active life at the command line, and various supplementary scripts can help you perform some functions more easily there than on the desktop.

LibreOffice and Apache OpenOffice are primarily desktop applications. However, you might not know that the way these applications open is determined by command-line options. You can also print and convert files from the command line, in some cases more easily than from the desktop. Additionally, of the various supplementary scripts that have been written for their predecessor OpenOffice.org, at least two sets of utilities continue to be useful for manipulating their files from the command line.

You may need to search for these utilities, because they are not packaged for all distributions. Still, whether you are a home user or a developer who wants to summon LibreOffice as part of a more elaborate script, these tools can be worth hunting down. Like many command-line tools, they allow you to perform functions more easily than on the desktop or do things that are not even options in the editing window.

LibreOffice and OpenOffice Command Options

Both LibreOffice and Apache OpenOffice start with the command soffice, which is short for StarOffice, the name of the application before Sun Microsystems released the code in July 2000. Some of the options listed by the --help option are obsolete, such as --quickstart and --nofirststartwizard, which refer to features no longer in recent versions. Others, like --display, are useful only on Linux, but many of the others are useful on any platform.

The most obvious way to modify this basic command is with options that start with a specific module: --writer for Writer, --calc for Calc, --draw for Draw, --impress for Impress, and --math for Math. Less obvious is --web, which opens Writer in the Web Layout view, and --global, which opens a new master document in Writer.

If you want to start work directly, you can open an existing file with -o FILENAME or a read-only copy with --view FILENAME. Alternatively, if you know the template you want to use, -n TEMPLATE opens a new document using the template specified. Similarly, you can start a slide show directly – instead of opening it to edit – with --show FILENAME.odp.

Developers might be interested in --headless, which opens the application without a GUI, or at least --invisible, which opens the application so its features are open only via the API. All users might be interested in the --nologo option to start LibreOffice or OpenOffice without a splash screen or --norestore to disable file recovery after a crash to ensure a slightly faster – or at least less annoying – startup time.

Other options let you print files without opening the editing window. Using -p, you can print a space-separated list of files to the default print. Alternatively, you can specify a printer with --pt PRINTERNAME to print a space-separated list of files wherever you want. Neither returns any output to the command-line prompt.

Still another option is --print-to-file PRINTERNAME OUTPUTDIRECTORY FILES, which is useful for generating PostScript files (as long as the printer supports PostScript, of course). Interestingly enough, --print-to-file supports space-separated lists of all parameters, which means that different printers can print different files to different directories all at the same time.

The command line also supports conversion of files from one supported format to another with --convert OUTPUTFILEEXTENSION OUTPUTDIRECTORY FILES. You can use any file extension listed in the Save or Save As dialogs for LibreOffice or OpenOffice. If no output directory is specified, then the output files are created in the present working directory. This option is the equivalent of File | Wizards | Document Converter.

OpenOffice.org Python

Over the years, some of the utilities written for OpenOffice.org or its successors have become obsolete as features and circumstances have changed. However, one utility that has proved long-lived is OpenOffice.org Python (OOoPy). OOoPy [1] is a collection of Python scripts for working with LibreOffice and OpenOffice files. It is best run with Python 2.5 or higher, although you can run it with Python 2.3 plus the ElementTree library [2].

To use the scripts, download the OOoPy file and unarchive it. From the directory in which you placed the file, run the command:

python setup.py install

The directory ~/.local/share/ooopy will be created. You can either run the scripts from the /bin subdirectory of the directory to which you downloaded, or you add the directory to your environment.

Some of the scripts, such as ooo_fieldreplace, ooo_mailmerge, and ooo_prettyxml, are of interest mainly to Python developers and are also seriously underdocumented. The remainder, with their triple Os, underscores, and help confined to the bare structure displayed by --help, take careful typing, but they are well worth the effort.

For example, if you want to extract text from a document, you can use ooo_as_text with the syntax:

ooo_as_text --output-file OUTPUTFILE INPUTFILE, INPUTFILE

Most likely, you will want to add -n to add a line to mark the end of each paragraph. Other formatting will be lost, but the output can be read in any text editor. The script may be able to recover a corrupted file, and, in some cases, this may be the easiest way to begin converting a file to another format.

The ooo_as_text script is also used alongside GNU grep to run ooo_grep. Using ooo-grep does not give you all the options of grep, but it does allow you to search files with all the regular expressions that grep supports. Its results are the same as those of grep -l; that is, it returns the names of files that contain matches.

Another useful script is ooo_cat, which combines a comma-separated list of files into a single file. Aside from the command, ooo_cat has the same structure as ooo_as_text.

Writer2LaTeX

Writer2LaTeX [3] has outgrown its name. Originally, it was a Java program for converting documents produced in LibreOffice's or OpenOffice's word processor to LaTeX, which many users prefer for layout. Now, however, the program also includes options for converting bibliographic data to BibTeX format, Writer files to ePub, and Writer and Calc files to XHTML. This growth is reflected in a 73-page manual [4]. The basic command structure for Writer2LaTeX is:

w2l INPUTFILE OUTPUTFILE

By default, the conversion is to LaTeX. However, you can specify formats with the options -latex, -bibtex, -xhtml, -epub, and others (Figure 1). Unlike the OOoPy scripts, Writer2LaTeX does display status reports at the command line.

Figure 1: Writer2LaTeX converts LibreOffice and OpenOffice information into several different formats.

The formatting of LaTeX files specifically can also be detailed. With -ultraclean, most formatting is ignored. Using -clean, hyperlinks, color, and limited formatting is preserved. Additionally, you can specify whether the LaTeX file is optimized to produce a PDF file for viewing on the screen with -pdfscreen or for printing with -pdfprint.

Similarly, conversions to XHTML can be adjusted by selecting a template with --template FILE or a style sheet with --stylesheet FILE. The documentation also includes more than 45 pages about creating style sheets and templates to create output files that are formatted with more precision.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News