Search code examples
pythonpython-3.xrtf

Easiest way to create an rtf file in Python?


I have tried using,

with open(myfile.rtf, 'a') as file:
    file.write("{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard")
    file.write(my_String_here)
    file.write("\par}")

I have briefly looked at PyRTF but wasn't able to find an example that works with Python3


Solution

  • Your problem is probably that \r and similar characters are treated as escape characters. Use raw strigns instead, eg write(r'{\rtf1....')