📝 智能文本分割工具介绍
🎯 核心功能
专业文本分割处理器 - 按标点智能分割大文本文件,保持语义完整性
✨ 主要特点
- 🔪 智能分割
- 每份约4200字符(可调整)
- 优先在标点符号处分割(支持12种中英文标点)
- 自动处理超长段落(无标点时强制分割)
- 🧹 空格处理
- 精确删除单空格(保留连续空格和换行符)
- 保持文本原始排版结构
- 📁 文件管理
- 自动生成序列文件名(1111.txt, 1112.txt…)
- 自动创建输出目录(input文件夹)
- 冲突处理(自动跳过已存在编号)
- 🖥 交互体验
- 拖拽文件即可操作
- 实时显示处理进度
- 错误自动捕获并提示
🛠️ 技术亮点
- 多标点识别(,。,.!?!?;;)
- 精确空格处理正则表达式
(?<!\s)\ (?!\s)
- 跨平台兼容(Windows/macOS/Linux)
- 完善的异常处理机制
🚀 使用流程
- 拖拽TXT文件到窗口
- 自动处理并显示分割结果
- 生成序列文件到input目录
- 自动退出(无残留)
💡 专业建议:适合处理日志文件、长篇小说、论文等大文本,保持段落完整性的同时实现均匀分割。
import os
import re
import sys
from pathlib import Path
def clean_single_spaces(text):
"""精确删除单空格(保留换行符、制表符等)"""
return re.sub(r'(?<!\s)\ (?!\s)', '', text)
def split_at_punctuation(text, max_length=4200):
"""在标点符号处分割文本"""
text = clean_single_spaces(text)
chunks = []
while len(text) > max_length:
split_pos = max(
text.rfind('.', 0, max_length),
text.rfind('。', 0, max_length),
text.rfind('!', 0, max_length),
text.rfind('!', 0, max_length),
text.rfind('?', 0, max_length),
text.rfind('?', 0, max_length),
text.rfind(',', 0, max_length),
text.rfind(',', 0, max_length),
text.rfind(';', 0, max_length),
text.rfind(';', 0, max_length),
text.rfind('\n', 0, max_length)
)
split_pos = split_pos if split_pos != -1 else max_length
chunks.append(text[:split_pos+1].strip())
text = text[split_pos+1:]
if text.strip():
chunks.append(text.strip())
return chunks
def get_next_filename(output_dir):
"""获取下一个序列文件名"""
n = 1111
while os.path.exists(os.path.join(output_dir, f"{n}.txt")):
n += 1
return n
def process_file(input_path, output_dir):
"""处理单个文件"""
with open(input_path, 'r', encoding='utf-8') as f:
content = f.read()
chunks = split_at_punctuation(content)
start_num = get_next_filename(output_dir)
for i, chunk in enumerate(chunks):
output_path = os.path.join(output_dir, f"{start_num + i}.txt")
with open(output_path, 'w', encoding='utf-8') as f:
f.write(chunk)
return len(chunks), start_num
def main():
print("="*40)
print(" 文本分割工具 ".center(40, '='))
print("="*40)
print("\n请将txt文件拖拽到本窗口,按回车确认")
filepath = input("\n拖拽文件到此 > ").strip('" \n')
if not filepath.lower().endswith('.txt'):
print("\n错误:仅支持txt文件")
sys.exit(1)
try:
output_dir = os.path.join(os.path.dirname(filepath), "input")
os.makedirs(output_dir, exist_ok=True)
chunk_count, start_num = process_file(filepath, output_dir)
print(f"\n分割完成!生成文件:")
print(f"{start_num}.txt 到 {start_num + chunk_count - 1}.txt")
print(f"输出路径:\n{output_dir}")
print("\n自动退出...")
sys.exit(0)
except Exception as e:
print(f"\n错误: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
import time # 新增延时退出功能
main()
