跳到内容
🛠️ToolsShed

Text Columns

将文本分割成多个等宽列以便阅读。

关于此工具

文本分栏是一个简单的工具,可以将长文本分成多个等宽栏目,使长篇文章更易阅读和扫描。这种分栏布局在报纸和书籍中很普遍,通过限制每行的宽度来减轻眼睛疲劳,帮助读者在栏目间移动时保持专注。

使用这个工具非常简单,只需将文本粘贴到输入区域并选择所需的栏目数量,工具就会自动将文本均匀分配到各栏目中。这对于格式化文章、诗歌、随笔或任何冗长的文本都非常有效,可以显著提高可读性。

文本分栏特别适合用于准备出版内容、设计新闻通讯或将长文档重新格式化以增强视觉吸引力。这种多栏目布局也能很好地适应桌面和移动设备,在不同屏幕尺寸上都能平稳调整。

常见问题

代码实现

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.