10

If you are writing to a file with python, is there any way to make certain parts of the text bold, italic, or underlined ?

i tried:

test = '/location/tester.rtf'
out_file = open(test,'w')
out_file.write('is this {\bold}?')
out_file.close() #thanks to the comment below

is it possible to write FORMATTED TEXT like bold, italic, or underlined text via python ? i feel like .rtf is the most basic formatted text but correct me if i'm wrong

O.rka
  • 29,847
  • 68
  • 194
  • 309
  • 4
    1) Ask the real question, in the title: "How can an RTF file be written in Python?" (Or whatever the desired format is; e.g. DOC, DOCX, whatever Open Office uses, etc.) and 2) Include the error symptoms (which usually includes error messages). – user2246674 Apr 23 '13 at 06:01
  • 1
    You have to `out_file.close()` before you can open it again. – Volatility Apr 23 '13 at 06:01

1 Answers1

10

Just been playing around with this assuming MS word, I found that you needed to wrap the document in '{}' and define the doctype, then start bold with '\b' and end with '\b0'. An example would be

test = 'tester.rtf'
out_file = open(test,'w')
out_file.write("""{\\rtf1
This is \\b Bold  \\b0\line\
}""")
out_file.close() #thanks to the comment below

Note the double '\' since python has special meanings for '\b' and '\r'.

The full info came from http://www.pindari.com/rtf1.html, which also describes italics, font etc.

Let me know if that worked for you.

max k.
  • 549
  • 4
  • 9