From 73e671a6ce86308d61b95264953aa79d63d30cdc Mon Sep 17 00:00:00 2001 From: James Simmons Date: Sun, 06 Sep 2009 22:17:00 +0000 Subject: modified: ReadEtextsActivity.py modified: rtfconvert.py Support conversion of RTF documents. --- diff --git a/ReadEtextsActivity.py b/ReadEtextsActivity.py index c40ec76..f77184d 100644 --- a/ReadEtextsActivity.py +++ b/ReadEtextsActivity.py @@ -41,6 +41,7 @@ import cPickle as pickle import speech import xopower +import rtfconvert PAGE_SIZE = 38 TOOLBAR_READ = 2 @@ -796,6 +797,14 @@ class ReadEtextsActivity(activity.Activity): i = i + 1 else: current_file_name = filename + + if rtfconvert.check(current_file_name): + converted_file_name = os.path.join(self.get_activity_root(), 'instance', + 'convert%i' % time.time()) + rtfconvert.convert(current_file_name, converted_file_name) + os.remove(current_file_name) + current_file_name = converted_file_name + self.tempfile = converted_file_name self.etext_file = open(current_file_name,"r") diff --git a/rtfconvert.py b/rtfconvert.py index 8b61cb1..2e875f0 100755 --- a/rtfconvert.py +++ b/rtfconvert.py @@ -21,10 +21,21 @@ import sys # This is a script to take the a file in RTF format and convert it to a text file readable by Read Etexts. -def convert(file_path): +def check(file_path): rtf_file = open(file_path,"r") - out = open("book.txt", 'w') + line = rtf_file.readline() + rtf_file.close() + + if line.startswith('{\\rtf1'): + return True + else: + return False + +def convert(file_path, output_path): + + rtf_file = open(file_path,"r") + out = open(output_path, 'w') brace_count = 0 while rtf_file: @@ -78,7 +89,11 @@ def count_braces(string): if __name__ == "__main__": try: opts, args = getopt.getopt(sys.argv[1:], "") - convert(args[0]) + if check(args[0]): + print 'It is an RTF file' + convert(args[0], args[1]) + else: + print 'It is NOT an RTF file' except getopt.error, msg: print msg print "This program has no options" -- cgit v0.9.1