RTF

By Hal Canary, 2008-11-02 08:27:52 (link)
#computers-code

I was reading the other day the submission guidelines for a certain publishing house. They wanted RTF documents. One would think plain text would be just as good, but it's easy enough to write a program that converts a utf8-encoded plain text document to a simple RTF document. And the output is about a third of the size of the document that you would get from using oowriter.

#!/usr/bin/env python
# txt2rtf.py
# Written 2008 Hal Canary
# Dedicated to the Public Domain
import sys
fi = sys.stdin
fo = sys.stdout
encoding = 'UTF-8'
fo.write(r'{\rtf1\ansi\deff0{\fonttbl{\f0 Times New Roman;}}')
fo.write(r'\paperh15840\paperw12240\margl1800\margr1800\margt1440\margb1440\fs24')
fo.write('\n')
for line in fi:
  uline = unicode(line, encoding)
  for char in uline:
    if (char == "\n"):
      fo.write("\\par\n")
      continue
    if (char == "\t"):
      fo.write("\\tab ")
      continue
    if (char == "\\"):
      fo.write("\\\\")
      continue
    if (char == "{"):
      fo.write("\\{")
      continue
    if (char == "}"):
      fo.write("\\}")
      continue
    ordchar = ord(char)
    if (ordchar >= 128):
      fo.write("\\u%d?" % ordchar)
      continue
    fo.write(char)
fo.write('}\n\n')

For a manuscript, you don't want to do any fancy formating, bold text, fonts, colors, curly quotes, et cetera. This avoids all of that jazz. One might want italic text, but that's not fASCIIst enough, is it?


(back)