Row format¶
This article demonstrates row-level formatting in rtflite: borders, cell alignment, column widths, and text formatting to create professional tables.
Overview¶
Row-level formatting provides granular control over table appearance:
- Border styles (single, double, thick)
- Column width control with relative sizing
Imports¶
Border styles¶
PDF conversion limitation
Prefer reviewing the .rtf
output. Some border styles may not render
perfectly in PDF due to LibreOffice conversion quirks.
Demonstrate different border types:
# Create border demonstration data from BORDER_CODES
border_data = [
[border_type, f"Example of {border_type or 'no'} border"]
for border_type in rtf.attributes.BORDER_CODES.keys()
]
df_borders = pl.DataFrame(
border_data, schema=["border_type", "description"], orient="row"
)
# Apply different border styles to each row
doc_borders = rtf.RTFDocument(
df=df_borders,
rtf_body=rtf.RTFBody(
border_bottom=tuple(rtf.attributes.BORDER_CODES.keys()),
),
)
doc_borders.write_rtf("row-border-styles.rtf")
Column widths¶
Control relative column widths using col_rel_width
:
# Create width demonstration data
width_demo = [
["Narrow", "Standard Width", "Wide Column"],
["1.0", "2.0", "3.0"],
["Small", "Medium text content", "Much wider column for longer text"],
]
df_widths = pl.DataFrame(width_demo, schema=["narrow", "standard", "wide"])
# Apply different column width ratios
doc_widths = rtf.RTFDocument(
df=df_widths,
rtf_body=rtf.RTFBody(
col_rel_width=[1.0, 2.0, 3.0], # Relative width ratios
),
)
doc_widths.write_rtf("row-column-widths.rtf")