rtfunicode

2.3 · active · verified Thu Apr 16

rtfunicode is a Python library (version 2.3) that provides an encoder for Unicode to RTF 1.5 command sequences. It allows for generating valid RTF output that includes international characters by registering a new `rtfunicode` codec, which can then be used directly with `str.encode()`. The library is actively maintained, with a focus on modern Python versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import `rtfunicode` to register its codec, and then use the `encode('rtfunicode')` method on a standard Python string to convert Unicode characters into their RTF 1.5 command sequences.

import rtfunicode

# Encode a Unicode string to RTF using the 'rtfunicode' codec
unicode_string = 'Hello, RTF and unicode mix just fine! \u263A'
rtf_output = unicode_string.encode('rtfunicode')

print(f"Original Unicode: {unicode_string}")
print(f"Encoded RTF: {rtf_output!r}")

# The output includes the \uN? sequence for Unicode characters
# For example, \u263A (smiling face) becomes \u9786? (N=9786 in signed 16-bit int)
assert rtf_output == b'Hello, RTF and unicode mix just fine! \\u9786?'

view raw JSON →