Text format¶
This article demonstrates advanced text formatting capabilities in rtflite: fonts, colors, alignment, indentation, special characters, and common patterns for clinical documentation.
Overview¶
Advanced text formatting is essential for creating production-ready clinical documents that meet regulatory standards.
Key formatting features include:
- Text format styles (bold, italic, underline, superscript, subscript)
- Font sizes and alignment options (left, center, right, justified)
- Text colors and background colors
- Indentation and spacing control
- Special symbols and mathematical notation
- Inline formatting combinations
Imports¶
Text style¶
Demonstrate core text formatting options:
# Create formatting demonstration data
format_demo = [
["Normal", "", "Regular text", "Default body text"],
["Bold", "b", "Bold text", "Emphasis and headers"],
["Italic", "i", "Italic text", "Special terms, notes"],
["Bold Italic", "bi", "Bold italic text", "Maximum emphasis"],
["Underline", "u", "Underlined text", "Highlight important items"],
["Strikethrough", "s", "Crossed out", "Deprecated content"],
]
df_formats = pl.DataFrame(
format_demo, schema=["format_type", "code", "example", "usage"], orient="row"
)
print(df_formats)
shape: (6, 4)
┌───────────────┬──────┬──────────────────┬───────────────────────────┐
│ format_type ┆ code ┆ example ┆ usage │
│ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str ┆ str │
╞═══════════════╪══════╪══════════════════╪═══════════════════════════╡
│ Normal ┆ ┆ Regular text ┆ Default body text │
│ Bold ┆ b ┆ Bold text ┆ Emphasis and headers │
│ Italic ┆ i ┆ Italic text ┆ Special terms, notes │
│ Bold Italic ┆ bi ┆ Bold italic text ┆ Maximum emphasis │
│ Underline ┆ u ┆ Underlined text ┆ Highlight important items │
│ Strikethrough ┆ s ┆ Crossed out ┆ Deprecated content │
└───────────────┴──────┴──────────────────┴───────────────────────────┘
Apply text formatting using a column-based approach:
Tip
Use tuples ()
to specify per-row attributes.
# Apply text formatting by row
doc_formats = rtf.RTFDocument(
df=df_formats,
rtf_body=rtf.RTFBody(
text_format=("", "b", "i", "bi", "u", "s"),
),
)
doc_formats.write_rtf("text-format-styles.rtf")
Font size and alignment¶
Demonstrate font size variations and text alignment:
# Create font size and alignment data
font_align_demo = [
["Left", "12pt", "l"],
["Center", "14pt", "c"],
["Right", "10pt", "r"],
["Justified", "11pt", "j"],
]
df_font_align = pl.DataFrame(
font_align_demo, schema=["alignment", "size", "text_justification"], orient="row"
)
print(df_font_align)
shape: (4, 3)
┌───────────┬──────┬────────────────────┐
│ alignment ┆ size ┆ text_justification │
│ --- ┆ --- ┆ --- │
│ str ┆ str ┆ str │
╞═══════════╪══════╪════════════════════╡
│ Left ┆ 12pt ┆ l │
│ Center ┆ 14pt ┆ c │
│ Right ┆ 10pt ┆ r │
│ Justified ┆ 11pt ┆ j │
└───────────┴──────┴────────────────────┘
# Apply font sizes and alignment
doc_font_align = rtf.RTFDocument(
df=df_font_align,
rtf_body=rtf.RTFBody(
text_justification=("l", "c", "r", "j"),
text_font_size=(12, 14, 10, 11),
),
)
doc_font_align.write_rtf("text-font-size-alignment.rtf")
Text color¶
Demonstrate text and background color applications:
# Create color demonstration data
color_demo = [
["Normal", "Black text on white"],
["Warning", "Orange text for caution"],
["Error", "Red text for alerts"],
["Info", "Blue text for information"],
]
df_colors = pl.DataFrame(color_demo, schema=["status", "description"], orient="row")
print(df_colors)
shape: (4, 2)
┌─────────┬───────────────────────────┐
│ status ┆ description │
│ --- ┆ --- │
│ str ┆ str │
╞═════════╪═══════════════════════════╡
│ Normal ┆ Black text on white │
│ Warning ┆ Orange text for caution │
│ Error ┆ Red text for alerts │
│ Info ┆ Blue text for information │
└─────────┴───────────────────────────┘
# Apply text colors
doc_colors = rtf.RTFDocument(
df=df_colors,
rtf_body=rtf.RTFBody(
text_color=("black", "orange", "red", "blue"),
),
)
doc_colors.write_rtf("text-color.rtf")
Indentation¶
Show indentation options for hierarchical content (values are in twips):
# Create indentation demonstration data
indent_demo = [
["Main section", "No indent"],
["First level subsection", "300 twips indent"],
["Second level detail", "600 twips indent"],
["Third level item", "900 twips indent"],
]
df_indent = pl.DataFrame(indent_demo, schema=["level", "description"], orient="row")
print(df_indent)
shape: (4, 2)
┌────────────────────────┬──────────────────┐
│ level ┆ description │
│ --- ┆ --- │
│ str ┆ str │
╞════════════════════════╪══════════════════╡
│ Main section ┆ No indent │
│ First level subsection ┆ 300 twips indent │
│ Second level detail ┆ 600 twips indent │
│ Third level item ┆ 900 twips indent │
└────────────────────────┴──────────────────┘