Is there a Python module for converting RTF to plain text?

Question:

Ideally, I’d like a module or library that doesn’t require superuser access to install; I have limited privileges in my working environment.

Asked By: Tony

||

Answers:

There is good library pyrtf-ng for all-purpose RTF handling.

Answered By: cleg

Have you checked out pyrtf-ng?

Update: The parsing functionality is available if you do a Subversion checkout, but I’m not sure how full-featured it is. (Look in the rtfng.parser.base module.)

Answered By: Vinay Sajip

OpenOffice has a RTF reader. You can use python to script OpenOffice, see here for more info.

You could probably try using the magic com-object on Windows to read anything that smells ms-binary. I wouldn’t recommend that though.

Actually parsing the raw data probably won’t be very hard, see this example written in .bat/QBasic.

DocFrac is a free open source converter betweeen RTF, HTML and text. Windows, Linux, ActiveX and DLL platforms available. It will probably be pretty easy to wrap it up in python.

RTF::TEXT::Converter – Perl extension for converting RTF into text. (in case You have problems withg DocFrac).

Official Rich Text Format (RTF) Specifications, version 1.7, by Microsoft.

Good luck (with the limited privileges in Your working environment).

Answered By: Paweł Polewicz

I ran into the same thing ans I was trying to code it myself. It’s not that easy but here is what I had when I decided to go for a commandline app. Its ruby but you can adapt to python very easily.
There is some header garbage to clean up, but you can see more or less the idea.

f = File.open('r.rtf','r')
 b=0
 p=false
 str = ''
 begin
    while (char = f.readchar)
        if char.chr=='{'
   b+=1 
   next
  end
        if char.chr=='}'
   b-=1 
   next
  end
  if char.chr=='\'
   p=true
   next
  end
  if p==true && (char.chr==' ' or char.chr=='n' or char.chr=='t' or char.chr=='r')
   p=false 
   next
  end
  if p==true && (char.chr==''')
#this is the source of my headaches. you need to read the code page from the header and encode this.
   p=false 
   str << '#'
   next
  end
  next if b>2
  next if p
  str << char.chr
    end
rescue EOFError
end
f.close
Answered By: Josep Valls

I’ve been working on a library called Pyth, which can do this:

http://pypi.python.org/pypi/pyth/

Converting an RTF file to plaintext looks something like this:

from pyth.plugins.rtf15.reader import Rtf15Reader
from pyth.plugins.plaintext.writer import PlaintextWriter

doc = Rtf15Reader.read(open('sample.rtf'))

print PlaintextWriter.write(doc).getvalue()

Pyth can also generate RTF files, read and write XHTML, generate documents from Python markup a la Nevow’s stan, and has limited experimental support for latex and pdf output. Its RTF support is pretty robust — we use it in production to read RTF files generated by various versions of Word, OpenOffice, Mac TextEdit, EIOffice, and others.

Answered By: Brendon

Conversely, if you want to write RTFs easily from Python, you can use the third-party module rtflib. It’s a fairly new and incomplete module but still very powerful and useful. Below is an example that writes “hello world” in rich text to an RTF called helloworld.rtf. This is a very primitive example, and the module can also be used to add colors, italics, tables, and many other aspects of rich text to RTF files.

from rtflib import *
file = RTF("helloworld.rtf")
file.startfile()
file.addstrict()
file.addtext("hello world")
file.writeout()
Answered By: codedude

PyRTF-ng 0.9.1 has not parsed any of my RTF documents, both with the ParsingException.
First document was generated with OpenOffice 3.4, the second one with Mac TextEdit.

Pyth 0.5.6 parsed without problems both documents, but has not processed cyrillic symbols properly.

But each editor opens other’s editor document correctly and without trouble, so all libraries seems to have a weak rtf support.

So I’m writing my own parser with with blackjack and hookers.

(I’ve uploaded both files, so you can check RTF libraries by yourself: http://yadi.sk/d/RMHawVdSD8O9 http://yadi.sk/d/RmUaSe5tD8OD)

Answered By: Konstantin Nikitin

I just came across pyrtflib – there’s not much (any) documentation on it, it’s kinda a case of installing it and then using the inbuilt help() function to find out what’s available and what everything does.

Having said that in my little trial run of its rtf.Rtf2Html.getHtml() function it went well enough. I haven’t tried the Rtf2Txt function but given the simpler nature of converting rtf to plaintext it should do fine I’d expect.

Answered By: Blair

Here’s a link to a script that converts rtf to text using regex:
Regular Expression for extracting text from an RTF string

Also, and updated link on github:
Github link

Answered By: ChrisE

If you are on Mac , you can convert an RTF file file.rtf to TXT from the CLI like:

textutil -convert txt file.rtf
Answered By: Franco Piccolo
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.