Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/rtfconvert.py
diff options
context:
space:
mode:
authorJames Simmons <jim@simmons.olpc>2009-09-01 23:16:44 (GMT)
committer James Simmons <jim@simmons.olpc>2009-09-01 23:16:44 (GMT)
commitb5db8f0d6a6d492c7dee81946d7ea4f5ac6c0483 (patch)
tree6501c968b15c9198abd30a781ecd15dcb25201be /rtfconvert.py
parent7004c6f321f4a11f65add698eff1f16a08ec509f (diff)
modified: .gitignore
modified: MANIFEST modified: activity/activity.info new file: rtfconvert.py
Diffstat (limited to 'rtfconvert.py')
-rwxr-xr-xrtfconvert.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/rtfconvert.py b/rtfconvert.py
new file mode 100755
index 0000000..20cc4a2
--- /dev/null
+++ b/rtfconvert.py
@@ -0,0 +1,80 @@
+#! /usr/bin/env python
+
+# Copyright (C) 2008 James D. Simmons
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+import getopt
+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 main(file_path):
+
+ rtf_file = open(file_path,"r")
+ out = open("book.txt", 'w')
+ brace_count = 0
+
+ while rtf_file:
+ line = rtf_file.readline()
+ if not line:
+ break
+ line = line.replace('\\pard', '\n')
+ line = line.replace('\\par', '\n')
+ line = line.replace(' \\i0', '*')
+ line = line.replace('\\i ', '*')
+ line = line.replace('\\emdash', '--')
+ line = line.replace('\\line', '\n')
+ brace_count = brace_count + count_braces(line)
+ line = strip_tags(line)
+ if brace_count == 1 and line.find('{') < 0 and line.find('}') < 0:
+ out.write(line.lstrip())
+ rtf_file.close()
+ out.close()
+ print "All done!"
+
+def strip_tags(string):
+ index = 0
+ copy = True
+ output = ''
+ while index < len(string):
+ if string[index] == '\\':
+ copy = False
+ if string[index] == ' ':
+ copy = True
+ if copy == True:
+ output = output + string[index]
+ index = index + 1
+ return output
+
+def count_braces(string):
+ index = 0
+ count = 0
+ while index < len(string):
+ if string[index] == '{':
+ count = count + 1
+ if string[index] == '}':
+ count = count - 1
+ index = index + 1
+ return count
+
+if __name__ == "__main__":
+ try:
+ opts, args = getopt.getopt(sys.argv[1:], "")
+ main(args[0])
+ except getopt.error, msg:
+ print msg
+ print "This program has no options"
+ sys.exit(2)