40

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

Tony
  • 2,955
  • 7
  • 26
  • 23

10 Answers10

50

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.

Brendon
  • 766
  • 7
  • 8
  • 14
    Shame it's not Python 3 compatible ;-( – Epoc Nov 10 '16 at 09:17
  • 4
    @Epoc, there is some work towards make it compatible to Python 3. I have one fork in my repo that you can install with `pip install git+https://github.com/robertour/pyth@pyth-py3`. You can see some of the discussion [here](https://github.com/brendonh/pyth/pull/33/commits). – toto_tico Sep 07 '17 at 14:28
  • 1
    In 2022, `pyth` still is only available for Python 2, and has not seen a release since 2014 – Flimm Jul 13 '22 at 08:36
7

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).

Paweł Polewicz
  • 3,711
  • 2
  • 20
  • 24
  • Thanks. I opened the document in OpenOffice and saved it as a plain text file. This was probably the simplest approach. And thanks for reminding me that it's My work environment. I asked for sudo access. – Tony Aug 28 '09 at 14:25
  • 2
    The link to RTF::TEXT::Converter is broken. So is the link to the discussion on the python mailing list. That is why link-answers are discouraged... – GreenAsJade Jun 07 '15 at 13:22
  • 1
    thanks for pointing it out, I fixed one of the links. Sadly the other one had to be deleted. – Paweł Polewicz Jun 07 '15 at 19:25
  • DocFrac still works, but does not support pt-br special chars. – Alan Tygel Jan 12 '19 at 21:20
  • 1
    Microsoft's RTF specification now lives at: http://download.microsoft.com/download/5/d/d/5dd33fdf-91f5-496d-9884-0a0b0ee698bb/%5BMS-OXRTFEX%5D.pdf – Julian Mehnle Sep 02 '20 at 03:24
  • 1
    @JulianMehnle that appears to be just extensions, not the whole spec. That's at https://interoperability.blob.core.windows.net/files/Archive_References/[MSFT-RTF].pdf – Mark Ransom Feb 22 '23 at 19:00
4

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

textutil -convert txt file.rtf
Franco Piccolo
  • 6,845
  • 8
  • 34
  • 52
3

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.)

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
2

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

Community
  • 1
  • 1
ChrisE
  • 376
  • 4
  • 14
1

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)

n611x007
  • 8,952
  • 8
  • 59
  • 102
1

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

cleg
  • 4,862
  • 5
  • 35
  • 52
  • Thanks, but the problem with pyrtf-ng is that it's useful for generating RTF files, not parsing them. I downloaded it from its SourceForge page (there is nothing under the Download tab at Google Code), and this is the only functionality I could find. – Tony Aug 26 '09 at 21:30
  • @tony, have you looked at http://code.google.com/p/pyrtf-ng/source/browse/#svn/trunk/rtfng/parser ? When there are no downloads yet on a Google Code hosted project, browse the sources!-) – Alex Martelli Aug 26 '09 at 21:38
1

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.

Blair
  • 176
  • 12
  • Have since given the Rtf2Txt.getText() function a go and it worked fine - my use of it was not an exhaustive edge-case torture test by any means but all the cases that I did test resulted in it giving me the expected output – Blair May 11 '15 at 14:29
-2

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
-2

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()
codedude
  • 71
  • 1