How to write RTF from Python?

Bengt Richter bokr at oz.net
Sat Apr 26 12:07:34 EDT 2003


On Fri, 25 Apr 2003 15:33:08 -0700, achrist at easystreet.com wrote:

>dsavitsk wrote:
>> 
>> Your requirements suggest to me that html is the best common denominator,
>> and it's easier to write.
>> 
>
>Yes, it is easier to write.  But I wrote a dead-plain-simple HTML file
>and opened it in MS Word (the version that came free on my computer in
>late 1996), and it looked just dreadful, with all kinds of visible
>markup that made no sense and generally might make you think that
>the  program that wrote it was written by someone who was not very
>smart.  So, I try to open the same file with Abiword (a relatively
>recent version), and it tells me that the file is bogus and refuses to
>open it at all.  This is simple HTML that opens fine in Netscape and in
>Amaya -- those have no trouble with my HTML (just HTML, BODY, UL, LI,
>TABLE, TR, TD, and B tags, IIRC).  So I figure that HTML is not a
>preferred path into word-processing programs when it is not known what
>version of which word-processing program the user has installed.  I
>intend to offer HTML output for those who want it, and I do have that
>nut cracked already, but it's not a complete solution.  
>
>So, again, wrt the larger nut, how to write RTF from Python? Any ideas?
>
You appear to be on NT, so there should be a RICHTX32.OCX somewhere
(which is a rich text text box) that can read and write rtf files and
also has methods and properties that allow you to mess with text.

Someone has probably interfaced to it via python and one of the win32
python wrappers, I would think.

Alternatively, if you just want a few features, use wordpad (start it by typing
write in a dos box, or use menu start>programs>accessories>wordpad) to make yourself
a template file and save it as .rtf. You can use %(placename)s in the text of the
template, so you can plug in substitution text using a dict, e.g.,
    template % {'placename':'replacement text', 'anotherplace':'etc'}

Here is an example python toy using an .rtf template prepared with
wordpad (aka write.exe) and writing the modified file and starting wordpad to show it:

====< rtfexample.py >========================
import sys, os
if len(sys.argv)<3:
    print 'Usage: rtfexample.py arg1 arg2'
    raise SystemExit
template = file('rtfexample.rtf').read().rstrip('\x00')
places = {'redtext':sys.argv[1], 'bluetext':sys.argv[2]}
file('rtfout.rtf','w').write(template % places)
os.system('start write rtfout.rtf')
=============================================
====< rtfexample.rtf >=======================
{\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\fmodern\fprq1 Courier New;}{\f3\froman Times New Roman;}}
{\colortbl\red0\green0\blue0;\red0\green0\blue255;\red255\green0\blue0;}
\deflang1033\horzdoc{\*\fchars }{\*\lchars }\pard\plain\f3\fs20 Plain text in default Times New Roman 10-point font.
\par \plain\f2\fs20 This line is courier new, and the following words
\par describe themselves:
\par \plain\f2\fs20\b bold
\par \plain\f2\fs20\i italic
\par \plain\f2\fs20\ul underlined
\par \plain\f2\fs20\b\i bold-and-italic\plain\f2\fs20 .
\par 
\par Above was a blank line.
\par 
\par Colored words on separate bullet lines:
\par \pard\li720\fi-720{\*\pn\pnlvlblt\pnf1\pnindent720{\pntxtb\'b7}}\plain\f2\fs20 {\pntext\f1\'b7\tab}Red  argv[1]: \plain\f2\fs20\cf2 %(redtext)s\plain\f3\fs20 
\par \plain\f2\fs20\cf0 {\pntext\f1\'b7\tab}Blue argv[2]: \plain\f2\fs20\cf1 %(bluetext)s\plain\f3\fs20 
\par \pard\plain\f2\fs20 This is the last line.
\par \pard\plain\f3\fs20 
\par }
=============================================

Without rtf features, the template text was
___________________________________________________

Plain text in default Times New Roman 10-point font.
This line is courier new, and the following words
describe themselves:
bold
italic
underlined
bold-and-italic.

Above was a blank line.

Colored words on separate bullet lines:
Red  argv[1]: %(redtext)s
Blue argv[2]: %(bluetext)s
This is the last line.
___________________________________________________

You don't have to have a monolithic template, obviously.
You could just snip pieces of boilerplate and commands
from an example containing features you want, and write
them out with write statements piece by piece or whatever.

HTH

Regards,
Bengt Richter




More information about the Python-list mailing list