Restart line numbering at 1 every 45 lines with page break
- Insert page break after 45 visual lines. - Reset line counter so each page starts at 1. - Uses embedded line numbers for consistent behavior across editors.
This commit is contained in:
+28
-1
@@ -460,12 +460,36 @@ def create_transcript_docx(text: str, filename: str):
|
||||
# Max characters per visual line (for 12pt Courier, 1" margins)
|
||||
max_chars = 58
|
||||
|
||||
# Global line counter for visual lines
|
||||
# Lines per page before restarting numbering
|
||||
lines_per_page = 45
|
||||
|
||||
# Current line counter for visual lines
|
||||
line_number = 0
|
||||
|
||||
# Split transcript into logical lines
|
||||
logical_lines = text.strip().splitlines()
|
||||
|
||||
def ensure_new_page_if_needed():
|
||||
nonlocal line_number
|
||||
if line_number >= lines_per_page:
|
||||
# Insert a page break paragraph
|
||||
p_break = doc.add_paragraph()
|
||||
# Page break via rPr: no line number, no text
|
||||
pPr = p_break._p.get_or_add_pPr()
|
||||
# Clear any existing line-related props
|
||||
for child in list(pPr):
|
||||
tag = child.tag.split("}")[-1] if "}" in child.tag else child.tag
|
||||
if tag in ("tabs", "spacing", "ind"):
|
||||
pPr.remove(child)
|
||||
# Add a page break run
|
||||
r = p_break.add_run()
|
||||
rPr = r._r.get_or_add_rPr()
|
||||
br = OxmlElement("w:br")
|
||||
br.set("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}type", "page")
|
||||
rPr.append(br)
|
||||
# Reset line counter for new page
|
||||
line_number = 0
|
||||
|
||||
for line in logical_lines:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
@@ -498,6 +522,8 @@ def create_transcript_docx(text: str, filename: str):
|
||||
|
||||
# First visual line: include label if present, ensuring total <= max_chars
|
||||
if content_lines:
|
||||
ensure_new_page_if_needed()
|
||||
|
||||
first_content = content_lines.pop(0)
|
||||
if label_text:
|
||||
prefix = label_text + " "
|
||||
@@ -521,6 +547,7 @@ def create_transcript_docx(text: str, filename: str):
|
||||
|
||||
# Subsequent visual lines: no label, just content
|
||||
for cl in content_lines:
|
||||
ensure_new_page_if_needed()
|
||||
line_number += 1
|
||||
_add_transcript_paragraph(doc, cl, line_number=line_number)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user