Translate markdown headings to WYSIWYG styles in summary .docx
This commit is contained in:
+38
-1
@@ -365,6 +365,11 @@ def create_transcript_docx(text: str, filename: str):
|
|||||||
def create_summary_docx(text: str, filename: str):
|
def create_summary_docx(text: str, filename: str):
|
||||||
"""
|
"""
|
||||||
Create a .docx summary with consistent font.
|
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.
|
No section headings; use bold/underline only.
|
||||||
"""
|
"""
|
||||||
doc = Document()
|
doc = Document()
|
||||||
@@ -373,8 +378,40 @@ def create_summary_docx(text: str, filename: str):
|
|||||||
font.name = "Courier"
|
font.name = "Courier"
|
||||||
font.size = Pt(12)
|
font.size = Pt(12)
|
||||||
|
|
||||||
|
heading_count = 0 # track headings in order
|
||||||
|
|
||||||
for line in text.splitlines():
|
for line in text.splitlines():
|
||||||
p = doc.add_paragraph(line)
|
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)
|
p.paragraph_format.space_after = Pt(4)
|
||||||
|
|
||||||
doc.save(filename)
|
doc.save(filename)
|
||||||
|
|||||||
Reference in New Issue
Block a user