RTF Writer

The writer emits deterministic RTF from the AST. It collects document resources such as fonts, colors, and list metadata, then walks blocks and inline nodes.

from rtfstruct import parse_rtf, to_rtf, write_rtf

document = parse_rtf(r"{\rtf1\ansi Hello}")
rtf = to_rtf(document)
write_rtf(document, "output.rtf")
rtfstruct.to_rtf(document)[source]

Export a document AST to RTF.

Parameters:

document (Document) – Document AST to export.

Returns:

RTF document text.

Return type:

str

rtfstruct.write_rtf(document, path)[source]

Write a document AST to an RTF file.

Parameters:
  • document (Document) – Document AST to export.

  • path (str | Path) – Destination file path.

Return type:

None

RTF writer for the document AST.

class rtfstruct.writer.RtfWriter(document)[source]

Write supported AST nodes as deterministic RTF.

Parameters:

document (Document)

export()[source]

Return the document as an RTF string.

Return type:

str

rtfstruct.writer.to_rtf(document)[source]

Export a document AST to RTF.

Parameters:

document (Document) – Document AST to export.

Returns:

RTF document text.

Return type:

str

rtfstruct.writer.write_rtf(document, path)[source]

Write a document AST to an RTF file.

Parameters:
  • document (Document) – Document AST to export.

  • path (str | Path) – Destination file path.

Return type:

None