From efb34dd9ff9c80c4618cca4be40bf3fbd3f3b7bc Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 14 Jun 2026 21:18:42 +0000 Subject: [PATCH] Translate markdown headings to WYSIWYG styles in summary .docx --- scraibe/email_sender.py | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/scraibe/email_sender.py b/scraibe/email_sender.py index 36d8756..cbb7a2b 100644 --- a/scraibe/email_sender.py +++ b/scraibe/email_sender.py @@ -365,6 +365,11 @@ def create_transcript_docx(text: str, filename: str): def create_summary_docx(text: str, filename: str): """ Create a .docx summary with consistent font. + Translates markdown headings into WYSIWYG formatting: + - First heading: bold + - Second heading: italic + - Third heading: underline + - Fourth heading: italic + underline No section headings; use bold/underline only. """ doc = Document() @@ -373,8 +378,40 @@ def create_summary_docx(text: str, filename: str): font.name = "Courier" font.size = Pt(12) + heading_count = 0 # track headings in order + for line in text.splitlines(): - p = doc.add_paragraph(line) - p.paragraph_format.space_after = Pt(4) + stripped = line.strip() + if not stripped: + continue + + # Detect markdown-style headings: #, ##, ###, #### + m = re.match(r"^(#{1,4})\s+(.*)", stripped) + if m: + heading_count += 1 + level = len(m.group(1)) # 1..4 + content = m.group(2).strip() + + p = doc.add_paragraph() + p.paragraph_format.space_after = Pt(4) + + run = p.add_run(content) + run.font.name = "Courier" + run.font.size = Pt(12) + + # Apply formatting based on this heading's ordinal position + if heading_count == 1: + run.bold = True + elif heading_count == 2: + run.italic = True + elif heading_count == 3: + run.underline = True + elif heading_count >= 4: + run.italic = True + run.underline = True + else: + # Normal text line + p = doc.add_paragraph(stripped) + p.paragraph_format.space_after = Pt(4) doc.save(filename)