Saturday, January 26, 2013

How to Automate Document Conversion With Unoconv in Linux

How to Automate Document Conversion With Unoconv in Linux

documents_pilesLibreOffice (and it’s predecessor, OpenOffice) has long been the tool of choice for creating text documents, spreadsheets, and presentations, and doing other office work on Linux. But while the Open Document Format works very well within open source programs, there will always come a time when you’ll need to exchange these files with someone on a proprietary platform. For one document, it’s no trouble to open LibreOffice and use “Save As…” to convert it to a suitable format such as .DOC or .DOCX. For the times when it’s not just one file, but three dozen, you can automate document conversion with unoconv.

unoconv is a command line program that does one thing: reads files in one format and writes them out in another. The program is able to automate document conversion by using an existing installation of Libre/OpenOffice to do the heavy lifting.

Installation

Installing unoconv is child’s play, as a stock Ubuntu install will include LibreOffice out of the box. Then, getting unoconv is as simple as the following command:

sudo apt-get install unoconv

Usage

With a straight-forward syntax, unoconv will help you make short work of any large conversion job.

First, it’s useful to know what sorts of files you can convert. You could open LibreOffice to check, but unoconv provides you a nice option to list these in the terminal:

You may need to scroll up a bit, as the list is pretty extensive. In order to convert from one format to another, you’ll need to use the following flags:

  • The “-f” flag will indicate what you’re requesting as the final output. For example, “-f pdf” will convert whichever file(s) you include in the command to PDF format.
  • The “-o” flag allows you to specify a different path for the converted files (however, this is optional, as the default is the current directory).

The command to convert all the MS Word (2000/2003) documents in the current directory (whether two or a hundred) to Open Document Text format and place them in the “proper_format” directory beneath the current directory would be as follows:

unoconv -f odt -o proper_format *.doc

Other Useful Unoconv Tips

Here are some other things to be aware of when using unoconv:

  • You can use the “-t” flag, foll owed by a path to a file, to apply that file as a template to all the files you convert.  These files should be the template format of whatever file type you’re converting to… for example, if (as in the example above) you’re converting a group of files to ODT format, the template files should be in ODT.
  • As mentioned above, unoconv uses LibreOffice to do the conversion… meaning that when you execute it from the command line, it needs to start an instance of LibreOffice, with all the waiting that sometimes entails.  So the command may take longer than you’d expect when you issue it.
  • If you’d like to avoid this delay, you can use the “-c” flag to connect to an instance of LibreOffice you might already have running.

Using unoconv can be a great time saver if you have a large project and need to reformat a long list of files to a proprietary format. The ability to automate document conversion is yet another reason to give your terminal a little respect.

Image credit: Paperwork Pile by BigStockPhoto

No comments:

Post a Comment

//PART 2