"""按完整表格行分段;表头重复,重叠行不跨表/工作表。""" def split_table_rows(header, rows, limit, overlap=0): limit, overlap = int(limit), int(overlap) if limit <= 0 or not 0 <= overlap < limit: raise ValueError("Invalid table splitting limits") chunks, current = [], [] length = len(header) for row in rows: if current and length + len(row) > limit: chunks.append({"content": header + "".join(current), "title": ""}) carry, carried = [], 0 if overlap: for previous in reversed(current): carry.insert(0, previous) carried += len(previous) if carried >= overlap: break # 必须留出至少一条新行的空间,不能只重复旧行。 while carry and len(header) + sum(map(len, carry)) + len(row) > limit: carry.pop(0) current = carry length = len(header) + sum(map(len, current)) current.append(row) length += len(row) if current: chunks.append({"content": header + "".join(current), "title": ""}) return chunks