-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Everyone,


I wasn't entirely satisfied with the Ms Word solution because it
requires the program run on Windows and have Office installed.

As an alternate solution, I created the program listed below to read a
file and create a RTF file format document from it.

The program is not platform dependent but it does require installation
of the PyRTF module.

I hope someone finds this useful/interesting.


#!C:\Python24\python
#
# program: rtfmaker.py
# Author:  Andrew Robert
#
# Function: Take a supplied file and dump its contents to an RTF file
#           This code is platform independant.
#
# Ver       Date       Programmer    Modification
# 1.0       07/21/06   AAR           Initial Creation
# 1.1                                added argument and source/target
file testing

import sys, time, string, stat, os
from PyRTF import *
from optparse import OptionParser

def MakeFile(lines):
        """
        Create the RTF file
        """

        doc     = Document()
        ss      = doc.StyleSheet
        section = Section()
        doc.Sections.append( section )

        # Append lines from source file
        for line in lines:
                section.append( line )

        return doc

def OpenFile( name ):
        return file( '%s' % name, 'w' )

def file_test(file):
        """
        Tests user supplied file to see if it exists and contains data.
        If the input file does not exist or is empty, return a warning code
        """
        if (0 == os.path.isfile(file) or (0 == os.stat(file)[stat.ST_SIZE])):
                return 1
        else:
                return 0

def run_tests(input, output):
        """
        Test source file to ensure it exists and contains data
        """
        if file_test(input) == 1 :
                print "\nError: Source file not found or is empty."
                parser.print_help()
                sys.exit(1)

        # Test target file to prevent accidental clobbering
        if file_test(output) == 0 :
                print "\nError: Target file already exists."
                parser.print_help()
                sys.exit(1)



def test_options(infile, outfile):
        """
        Verify that required values are specified on commmand line
        and that specified port is within range
        """

        if not infile :
                print "\nError: No input file specified"
                parser.print_help()
                sys.exit(1)
        
        if not outfile :
                print "\nError: No input file specified"
                parser.print_help()
                sys.exit(1)


if __name__ == '__main__' :

        parser=OptionParser()
        parser.add_option("-i", "--input" , dest="infile",
                  help="Input FILE" , metavar="FILE")
        parser.add_option("-o", "--output", dest="outfile",
                  help="Output file", metavar="FILE")
        
        (options, args) = parser.parse_args()

        test_options( options.infile , options.outfile )
        run_tests(options.infile , options.outfile)

        DR = Renderer()
        lines = open(options.infile,"r").readlines()
        rtffile = MakeFile(lines)

        # Write the new RTF file to disk
        DR.Write( rtffile, OpenFile( options.outfile ) )
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32)
Comment: GnuPT 2.7.2

iD8DBQFEwO9uDvn/4H0LjDwRAlHSAJ9KIzPYT2YHdK8MptBS8JSIdt8dZACgsfKi
nZQ3Bs71jsbFplmwbnVEIgI=
=ZPSo
-----END PGP SIGNATURE-----
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to