rtfunicode
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
-
LookupError: unknown encoding: rtfunicode
cause The 'rtfunicode' codec is not registered. This occurs when the `rtfunicode` module has not been imported.fixAdd `import rtfunicode` at the beginning of your script. Importing the module registers the codec. -
ERROR: Package 'rtfunicode' requires Python '>=3.10' but the running Python is 3.9.x
cause Attempting to install `rtfunicode` version 2.1 or newer on a Python environment older than 3.10.fixUpgrade your Python environment to 3.10 or newer. Alternatively, install a compatible older version like `pip install rtfunicode==2.2` for Python 3.9. -
SyntaxError: invalid syntax (when running code from rtfunicode)
cause rtfunicode versions 2.1+ utilize modern Python syntax (e.g., type annotations) compatible with Python 3.10+. Running these versions on older Python interpreters will result in syntax errors.fixEnsure your Python environment is 3.10 or newer when using rtfunicode versions 2.1+. Upgrade Python if necessary.
Warnings
- breaking rtfunicode version 2.3 dropped support for Python 3.9. Users on Python 3.9 must use `rtfunicode==2.2` or upgrade their Python environment.
- breaking rtfunicode version 2.1 and later require Python 3.10 or newer. Earlier versions (pre-2.1) supported Python 3.3-3.9. Attempting to install or run v2.1+ on Python versions older than 3.10 will fail.
- gotcha In version 2.1, rtfunicode was converted from a single module to a package. While the primary `import rtfunicode` remains functional, any tools or custom setups relying on `rtfunicode.py` as a direct file or internal module paths might require adjustments.
- gotcha The RTF standard uses `\uN?` where `N` is a signed 16-bit integer and `?` is a placeholder for older RTF readers. rtfunicode consistently sets this placeholder to `'?'`. If strict compliance with specific RTF readers requires a different fallback character (e.g., `\uN\'` plus a hex value), post-processing might be needed.
Install
-
pip install rtfunicode
Imports
- rtfunicode
import rtfunicode
Quickstart
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?'