import os from datetime import datetime from typing import Tuple from docx import Document from docx.shared import Inches from reportlab.lib.pagesizes import letter from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak from reportlab.lib.styles import getSampleStyleSheet from report_parser import parse_transcriptor_output from table_builder import build_all_tables from story_writer import generate_narrative def generate_narrative_report(csv_path: str, summary_path: str = None, interviewee_type: str = "Patient", report_style: str = "executive", llm_backend: str = "lmstudio", output_dir: str = "./outputs") -> Tuple[str, str, str]: print("[1/4] Parsing...") parsed_data = parse_transcriptor_output(csv_path, summary_path, interviewee_type) print("[2/4] Building tables...") tables = build_all_tables(parsed_data) print("[3/4] Generating narrative (1-2 min)...") narrative = generate_narrative(parsed_data, tables, report_style, llm_backend) print("[4/4] Creating outputs...") os.makedirs(output_dir, exist_ok=True) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") base = f"{output_dir}/narrative_report_{timestamp}" pdf = f"{base}.pdf" word = f"{base}.docx" html = f"{base}.html" create_pdf(narrative, tables, parsed_data, pdf) create_word(narrative, tables, parsed_data, word) create_html(narrative, tables, parsed_data, html) print(f"Done!\nPDF: {pdf}\nWord: {word}\nHTML: {html}") return pdf, word, html def create_pdf(narrative, tables, data, path): doc = SimpleDocTemplate(path, pagesize=letter) story = [] styles = getSampleStyleSheet() story.append(Paragraph("Narrative Research Report", styles['Title'])) story.append(Spacer(1, 0.3*72)) for section in narrative.split('\n\n'): if section.strip(): story.append(Paragraph(section.strip().replace('&','&').replace('<','<'), styles['BodyText'])) story.append(Spacer(1, 0.1*72)) doc.build(story) def create_word(narrative, tables, data, path): doc = Document() doc.add_heading('Narrative Research Report', 0) for section in narrative.split('\n\n'): if section.strip(): doc.add_paragraph(section.strip()) doc.save(path) def create_html(narrative, tables, data, path): html = f"""
{section.strip()}
" html += "" with open(path, 'w') as f: f.write(html)