Spaces:
Sleeping
Sleeping
| from docx import Document | |
| from docx.shared import Pt | |
| import json | |
| def _add_text(doc, text, bold=False): | |
| p = doc.add_paragraph() | |
| run = p.add_run(text) | |
| run.bold = bold | |
| run.font.size = Pt(11) | |
| def build_question_paper_docx(path, final_json, generator_raw, subject): | |
| doc = Document() | |
| doc.add_heading(f"Question Paper β {subject}", level=1) | |
| _add_text(doc, "Printable Version:", True) | |
| _add_text(doc, generator_raw[:8000]) | |
| # Structured table | |
| questions = final_json.get("final_qp", {}).get("questions", []) | |
| if questions: | |
| table = doc.add_table(rows=1, cols=5) | |
| hdr = table.rows[0].cells | |
| hdr[0].text = "Q.No" | |
| hdr[1].text = "SubQ" | |
| hdr[2].text = "Question" | |
| hdr[3].text = "CO" | |
| hdr[4].text = "Bloom/Tags" | |
| for q in questions: | |
| row = table.add_row().cells | |
| row[0].text = str(q.get("question_no", "")) | |
| row[1].text = str(q.get("sub_no", "")) | |
| row[2].text = q.get("question_text", "") | |
| row[3].text = q.get("course_outcome", "") | |
| row[4].text = q.get("bloom_level", "") + " | " + q.get("tags", "") | |
| doc.save(path) | |
| def build_answers_docx(path, final_json, subject): | |
| doc = Document() | |
| doc.add_heading(f"Answer Key β {subject}", level=1) | |
| answers = final_json.get("answers", {}) | |
| for k, v in answers.items(): | |
| _add_text(doc, f"{k}:", True) | |
| _add_text(doc, str(v)) | |
| doc.save(path) | |
| def build_obe_docx(path, final_json, subject): | |
| doc = Document() | |
| doc.add_heading(f"OBE Summary β {subject}", level=1) | |
| obe = final_json.get("obe", {}) | |
| _add_text(doc, json.dumps(obe, indent=2)) | |
| doc.save(path) | |