Ensure page break after line 29 and preserve all transcript text
- Insert page break after every 29 visual lines. - Wrap long lines instead of truncating to preserve all words. - No text is dropped; content wraps onto new lines as needed.
This commit is contained in:
+36
-8
@@ -528,29 +528,57 @@ def create_transcript_docx(text: str, filename: str):
|
|||||||
content_lines.append(current)
|
content_lines.append(current)
|
||||||
|
|
||||||
# First visual line: include label if present, ensuring total <= max_chars
|
# First visual line: include label if present, ensuring total <= max_chars
|
||||||
|
# If too long, split into multiple lines instead of dropping text.
|
||||||
if content_lines:
|
if content_lines:
|
||||||
ensure_new_page_if_needed()
|
ensure_new_page_if_needed()
|
||||||
|
|
||||||
first_content = content_lines.pop(0)
|
first_content = content_lines.pop(0)
|
||||||
if label_text:
|
if label_text:
|
||||||
prefix = label_text + " "
|
prefix = label_text + " "
|
||||||
# If too long, trim first_content at word boundary
|
# If prefix + first_content is too long, wrap first_content
|
||||||
if len(prefix) + len(first_content) > max_chars:
|
if len(prefix) + len(first_content) > max_chars:
|
||||||
allowed = max_chars - len(prefix)
|
allowed = max_chars - len(prefix)
|
||||||
if allowed < 1:
|
if allowed < 1:
|
||||||
allowed = 1
|
allowed = 1
|
||||||
# Truncate at word boundary
|
|
||||||
candidate = first_content[:allowed]
|
candidate = first_content[:allowed]
|
||||||
last_space = candidate.rfind(" ")
|
last_space = candidate.rfind(" ")
|
||||||
if last_space > 0:
|
if last_space > 0:
|
||||||
candidate = candidate[:last_space]
|
kept = candidate[:last_space]
|
||||||
first_content = candidate
|
rest = first_content[last_space:].strip()
|
||||||
first_line_text = prefix + first_content
|
else: # no space found; break mid-word to preserve everything
|
||||||
|
kept = candidate
|
||||||
|
rest = first_content[allowed:].strip()
|
||||||
|
|
||||||
|
first_line_text = prefix + kept
|
||||||
|
line_number += 1
|
||||||
|
_add_transcript_paragraph(doc, first_line_text, line_number=line_number)
|
||||||
|
|
||||||
|
# Prepend any remaining text as continuation lines
|
||||||
|
if rest:
|
||||||
|
# Re-wrap the rest into content_lines
|
||||||
|
extra_words = rest.split()
|
||||||
|
new_lines = []
|
||||||
|
buf = ""
|
||||||
|
for ew in extra_words:
|
||||||
|
if len(buf) == 0:
|
||||||
|
buf = ew
|
||||||
|
elif len(buf) + 1 + len(ew) <= max_chars:
|
||||||
|
buf += " " + ew
|
||||||
|
else:
|
||||||
|
new_lines.append(buf)
|
||||||
|
buf = ew
|
||||||
|
if buf:
|
||||||
|
new_lines.append(buf)
|
||||||
|
# Insert these before existing content_lines
|
||||||
|
content_lines = new_lines + content_lines
|
||||||
|
else:
|
||||||
|
first_line_text = prefix + first_content
|
||||||
|
line_number += 1
|
||||||
|
_add_transcript_paragraph(doc, first_line_text, line_number=line_number)
|
||||||
else:
|
else:
|
||||||
first_line_text = first_content
|
first_line_text = first_content
|
||||||
|
line_number += 1
|
||||||
line_number += 1
|
_add_transcript_paragraph(doc, first_line_text, line_number=line_number)
|
||||||
_add_transcript_paragraph(doc, first_line_text, line_number=line_number)
|
|
||||||
|
|
||||||
# Subsequent visual lines: no label, just content
|
# Subsequent visual lines: no label, just content
|
||||||
for cl in content_lines:
|
for cl in content_lines:
|
||||||
|
|||||||
Reference in New Issue
Block a user