PyRTF : create RTF file using Python

Rich Text Format (RTF) Document Generation in Python. To know more about PyRTF click here http://pyrtf.sourceforge.net/
Download
clck here to download pyrtf

Lets dive into the task. For this you need Python,Pyrtf installed in your pc. To install Python just ask google.
To install PyRTF just download from the given link. then unpack it any where then in Linux system open a terminal window go to the unpacked folder location and type python setup.py build
then python setup.py install
this should install PyRTF into your system.

Now I am giving an example where a RTF file can easily be created:

#!/usr/bin/env python
import sys
sys.path.append( ‘../’ )
from PyRTF import *
SIMPLE_TEXT=”this is simple pyrtf example….. hello pyrtf”

def test_rtf_generate():

    doc = Document()
    ss = doc.StyleSheet
    section = Section()
    doc.Sections.append( section )
    # text can be added directly to the section
    # a paragraph object is create as needed
    section.append( ‘Example 1′ )
    # blank paragraphs are just empty strings
    section.append( ” )
    # a lot of useful documents can be created
    # with little more than this
    section.append( ‘A lot of useful documents can be created ‘
    ‘in this way, more advance formating is available ‘
    ‘but a lot of users just want to see their data come out ‘
    ‘in something other than a text file.’ )
    return doc

def OpenFile( name ) :

    return file( ‘%s.rtf’ % name, ‘w’ )

if __name__ == ‘__main__’ :

    print “this is done by Tareq…….”
    DR = Renderer()
    doc3 = test_rtf_generate()
    DR.Write( doc3, OpenFile( ‘myrtf_doc’ ) )
    print “Finished”
    print “see in the current directory a myrtf_doc.rtf is created!!”

For your information it may cause problem if you just copy paste this code. You better write down in a file. That will make the code run easily. And your programming concept will also be clear by writing code. To learn a Programming language everyone should write down the codes rather than just copy pasting.
let me know if you face any problem running this code. Thanks for reading this.:)

About these ads

7 thoughts on “PyRTF : create RTF file using Python

  1. Hi,that was nice document but i have one query how can we integrate this with the plone any ideas are suggestions please . . .

  2. This is just the thing I’ve been looking for. I have written a lightweight markup language and want to be able to convert files to RTF. But I’m not quite clear from the example how it is used. Is there any documentation for it? Much appreciated. Thanks.

  3. I need to add a few different images and texts on different parts of my code into the RTF File. How can I insert more than one picture?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s