PyRTF3
PyRTF3 is a fork of the original PyRTF library, providing a Pythonic way to generate Rich Text Format (RTF) documents. It allows users to programmatically create complex RTF files including text, paragraphs, sections, tables, lists, and styling. The current version is 0.47.5, with releases occurring periodically to address issues and maintain compatibility.
Common errors
-
ModuleNotFoundError: No module named 'pyrtf3'
cause You are trying to import using the PyPI package name (`pyrtf3`) instead of the actual top-level package name (`pyrtf`).fixChange your import statements from `import pyrtf3` or `from pyrtf3 import ...` to `import pyrtf` or `from pyrtf.document import ...`. -
AttributeError: 'Document' object has no attribute 'save'
cause You are looking for a convenience method like `save()`, but `pyrtf3` uses a `write()` method that expects a file object.fixUse the `write()` method with an opened file object: `with open('output.rtf', 'wb') as f: doc.write(f)`. -
TypeError: write() missing 1 required positional argument: 'file'
cause You called `doc.write()` without providing the required file-like object, or you passed an incorrect argument type (e.g., a string path).fixProvide a file-like object opened in binary write mode, e.g., `with open('my_document.rtf', 'wb') as f: doc.write(f)`. -
ModuleNotFoundError: No module named 'PyRTF'
cause You are attempting to use import paths from the very old, unmaintained `PyRTF` library, which `pyrtf3` is a fork of.fixUpdate your import statements to use the `pyrtf` package structure, e.g., `from pyrtf.document import Document` instead of `import PyRTF` or `from PyRTF import Document`.
Warnings
- gotcha The PyPI package name is `pyrtf3`, but the top-level Python package you import is `pyrtf`. Attempting to `import pyrtf3` or `from pyrtf3 import ...` will result in a `ModuleNotFoundError`.
- breaking If migrating from the original `pyrtf` library (which is no longer maintained), import paths and some API structures have changed significantly. For example, `PyRTF.Document()` becomes `pyrtf.document.Document()`.
- gotcha The `Document.write()` method requires a binary file-like object (opened with 'wb'), not a string path. Passing a string path or a text file object will cause a `TypeError` or unexpected behavior.
Install
-
pip install pyrtf3
Imports
- Document
from pyrtf3.document import Document
from pyrtf.document import Document
- Section
from pyrtf.document import Section
- Paragraph
from pyrtf.document import Paragraph
- Text
from pyrtf.document import Text
- Table
from pyrtf.document import Table
Quickstart
import os
from pyrtf.document import Document, Section, Paragraph, Text
def create_simple_rtf():
doc = Document()
section = Section()
doc.append(section)
section.append(Paragraph(Text('Hello, PyRTF3!')))
section.append(Paragraph(Text('This is a simple RTF document generated with Python.')))
output_filename = 'simple_pyrtf3_document.rtf'
with open(output_filename, 'wb') as f:
doc.write(f)
print(f"RTF document '{output_filename}' created successfully.")
if __name__ == '__main__':
create_simple_rtf()