Text Columns
Split your text into multiple equal-width columns for easy reading.
HĂ€ufig gestellte Fragen
Code-Implementierung
import textwrap
def text_to_columns(text: str, num_columns: int, col_width: int = 30) -> str:
"""Split text into N columns with specified column width."""
lines = text.splitlines()
wrapped = []
for line in lines:
if line.strip():
wrapped.extend(textwrap.wrap(line, col_width) or [""])
else:
wrapped.append("")
# Pad to fill columns evenly
rows = -(-len(wrapped) // num_columns) # ceiling division
wrapped += [""] * (rows * num_columns - len(wrapped))
result_lines = []
for r in range(rows):
row_parts = []
for c in range(num_columns):
idx = c * rows + r
cell = wrapped[idx] if idx < len(wrapped) else ""
row_parts.append(cell.ljust(col_width))
result_lines.append(" ".join(row_parts))
return "\n".join(result_lines)
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " * 3
print(text_to_columns(text, num_columns=2, col_width=40))Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.