This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Tools & Utilities

Converting text files into ODF with odtwriter

By Dmitri Popov on October 10, 2007 (4:00:00 PM)

Share    Print    Comments   

While you can create and save documents in the OpenDocument format using OpenOffice.org, KWord, or AbiWord, there are other ways to generate ODF files. odtwriter, for example, can help you quickly convert plain text files formatted using reStructured Text markup into ODT (OpenOffice.org Writer-compatible ODF) documents. Using odtwriter, you can generate ODF files on machines that don't have ODF-compatible word processors installed, such as those running lightweight Linux distros, or simply compose documents in a text editor and leave the task of properly formatting them to odtwriter.

odtwriter is part of docutils, a set of tools for converting plain text files into other formats, such as HTML, XML, and LaTeX. This means that you can output the formatted text file into other formats besides ODF. This is a more efficient approach than creating an ODT document in Writer and then jumping through hoops to turn it into, for example, a clean HTML file.

odtwriter has a few features that are not available in OpenOffice.org. For example, OpenOffice.org doesn't support syntax highlighting, so making code blocks in a Writer document more legible and pretty is a non-trivial task. odtwriter, however, can apply syntax highlighting to the code blocks in the final ODT document.

The main drawback of using odtwriter to produce ODF documents is that you need to learn a new markup language. This is, however, not as bad as it might sound. Markup used in reStructured Text looks a lot like wiki markup, so you can pick up the basics in no time. The markup is also well-documented, with a reStructured Text Primer, Quick Reference, and cheat sheet available for your reading pleasure.

To make odtwriter work on your machine, you have to first install two packages: docutils and pygments. The latter is required only if you want the syntax highlighting functionality. On Ubuntu, installing both packages is as easy as running the sudo apt-get install python-docutils python-pygments command. Then, to install odtwriter, download its latest release, unpack it, and run the following commands:

python setup.py build
sudo python setup.py install

Using odtwriter is equally straightforward: the rst2odt.py command converts the specified source text file into an ODT document:

rst2odt.py text.txt document.odt

Like most command-line tools, odtwriter supports flags, and at least two of them can come in handy. By default, the syntax highlight feature in odtwriter is disabled; you need to use the --add-syntax-highlighting flag to turn it on:

rst2odt.py --add-syntax-highlighting text.txt document.odt

This applies Python syntax highlighting to the code blocks (marked as literal blocks) in the source text. For code blocks in other programming languages, you have to add so-called directives to the source text that activate alternative language (or lexer) highlighting:

.. sourcecode:: on
.. sourcecode:: Java

Since odtwriter relies on the Pygments utility for syntax highlighting, you can use any language supported by this software. You can find a list of all supported languages and their short names on the Pygments Web site.

Besides syntax highlighting, odtwriter can apply OpenOffice.org styles to the source text. When you run the rtst2odt.py command without specifying a style sheet, the resulting ODT document uses the default paragraph and character styles. However, odtwriter can use a special ODT file as a style sheet during conversion to apply styles to the marked text.

odtwriter allows you to specify the path to an ODT file containing styles that odtwriter uses during conversion. The style sheet file is a regular ODT document that contains a set of custom styles that all have the rststyle prefix: rststyle-textbody, rststyle-footer, rststyle-codeblock, and so on. odtwriter comes with a sample styles.odt style sheet file, which you can tweak to your liking by modifying the document in OpenOffice.org Writer. To point odtwriter to the style sheet, you can use either the --stylesheet or --stylesheet-path flags. The former allows you to specify the style sheet's location via its URL, while the latter can be used to specify the path to the style sheet file relative to the current working directory. For example, if the styles.odt file resides in your home directory, the command should be:

rst2odt.py --stylesheet-path=styles.odt text.txt document.odt

That's all there is to it. odtwriter by itself is simple in use, but its support for the reStructured Text markup and ability to use style sheets make it a powerful solution for generating ODF documents from plain text files.

Dmitri Popov is a freelance writer whose articles have appeared in Russian, British, US, German, and Danish computer magazines.

Share    Print    Comments   

Comments

on Converting text files into ODF with odtwriter

Note: Comments are owned by the poster. We are not responsible for their content.

Converting text files into ODF with odtwriter

Posted by: Anonymous [ip: 69.253.238.38] on October 10, 2007 04:38 PM
I'm actually impressed. Ever since I discovered that extracting text out of ODF files can be done with Python's standard modules, I was wondering when we'd see the opposite operation implemented. No doubt we'll see it in distros like DSL, Slax or Puppy.

#

Syntax highlighting

Posted by: Anonymous [ip: 69.248.236.96] on October 10, 2007 05:31 PM
Nice article!

Another approach for syntax highlighting:
highlight --syntax=python --inline-css sample.py > sample.html

OOWriter can then easily import the colored html.

The downside is that you can't tweak the appearance in Writer since all the styling is "direct formatting" in the code block.

#

"reStrecutured" ?

Posted by: Anonymous [ip: 82.192.250.149] on October 10, 2007 08:48 PM

The markup is also well-documented, with a reStrecutured Text Primer,

#

Converting text files into ODF with odtwriter

Posted by: Anonymous [ip: 83.8.198.158] on October 25, 2007 01:51 AM
Thanks for another very interesting article. Keep up the good work. Regards
<a href="http://www.profesjonalna-reklama.pl" target="_blank">Pozycjonowanie</a> <a href="http://www.topblogposts.info" title="Blog">Tom</a>

#

Converting text files into ODF with odtwriter

Posted by: Anonymous [ip: 89.59.191.72] on November 05, 2007 04:19 PM
Thank you for sharing this information
<a href="http://www.yellobook.eu">yellowpages</a>

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya