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

Work is fun!

Tagged with: , , , , ,
Posted in ploneCMS, python
7 comments on “PyRTF : create RTF file using Python
  1. Pach says:

    why dont in life do such work.. so that nobody cries?

  2. Srinivas HN says:

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

  3. raj says:

    can i connect postgreSQL with pyRTF

  4. tareqalam says:

    I am not sure … should be possible .. try google

  5. RobW says:

    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.

  6. tareqalam says:

    You need to install PyRTF first to run this script
    you can download from here http://pypi.python.org/pypi/PyRTF

  7. fani says:

    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

Disclaimer
All content provided on this "tareqalam.com" blog is for informational purposes only. The owner of this blog makes no representations as to the accuracy or completeness of any information on this site or found by following any link on this site. The owner of [tareqalam.wordpress.com] will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.
May 2008
M T W T F S S
« Apr   Jun »
 1234
567891011
12131415161718
19202122232425
262728293031  

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 17 other followers

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: