If you wish to use rtf2xml as part of a larger python program rather than from the command line, first import the rtf2xml.ParseRtf module:
import rtf2xml.ParseRtf
Next, create a parsing object:
try: parse_obj =rtf2xml.ParseRtf.ParseRtf( in_file = 'in.rtf', ) except rtf2xml.ParseRtf.RtfInvalidCodeException, msg: pass
The exception will catch any paths that don't exist. You will have to use the same exception when you run the method (below).
The in_file variable should point to the path of the RTF file. I'll cover the optional parameters below.
Finally, convert the document by calling the method 'parse_rtf'.
try: parse_obj.parse_rtf() except rtf2xml.ParseRtf.InvalidRtfException, msg: pass except rtf2xml.ParseRtf.RtfInvalidCodeException, msg: pass
The two exception classes are InvalideRtfException and RtfInvalidCodeException. If you set the level to something higher than 3 the script will raise an excetion for any errors. If the level is lower than 3, the script will only raise an exception for serious, non-recoverable errors.
Here is an example of a complete script that uses the rtf2xml modules. I have made minimial comments on the parameters one can use when call ing the 'parse_rtf' method. For more details, see the options in the full documentation, use.