Camelson/RNA
0
1from docx import Document2from docx.shared import Pt3from docx.enum.text import WD_ALIGN_PARAGRAPH4from docx.oxml import OxmlElement5from docx.oxml.ns import qn6import datetime7import re8 9 10# =========================11# RTL HELPERS12# =========================13 14def set_rtl(paragraph):15 """16 تفعيل RTL حقيقي للفقرة17 """18 p = paragraph._p19 pPr = p.get_or_add_pPr()20 21 bidi = OxmlElement('w:bidi')22 bidi.set(qn('w:val'), '1')23 pPr.append(bidi)24 25 26def set_cell_rtl(cell):27 """28 تفعيل RTL لكل الفقرات داخل خلية جدول29 """30 for paragraph in cell.paragraphs:31 paragraph.alignment = WD_ALIGN_PARAGRAPH.RIGHT32 set_rtl(paragraph)33 34 35def set_section_rtl(section):36 """37 جعل الصفحة بالكامل RTL38 """39 sectPr = section._sectPr40 bidi = OxmlElement('w:bidi')41 bidi.set(qn('w:val'), '1')42 sectPr.append(bidi)43 44 45# =========================46# MAIN FUNCTION47# =========================48 49def create_docx(clean_text: str, output_path: str):50 """51 إنشاء ملف DOCX طبي احترافي52 يدعم العربية و RTL بالكامل53 """54 55 doc = Document()56 57 # جعل الصفحة بالكامل RTL58 set_section_rtl(doc.sections[0])59 60 # =========================61 # إعداد الخط الافتراضي62 # =========================63 style = doc.styles['Normal']64 style.font.name = 'Cairo'65 style._element.rPr.rFonts.set(qn('w:eastAsia'), 'Cairo')66 style.font.size = Pt(12)67 68 # =========================69 # إعداد الهيدر70 # =========================71 header = doc.sections[0].header72 htable = header.add_table(1, 2, doc.sections[0].page_width)73 htable.autofit = True74 75 left_cell = htable.cell(0, 0)76 right_cell = htable.cell(0, 1)77 78 left_cell.text = "تحليل روشتة طبية"79 set_cell_rtl(left_cell)80 81 right_cell.text = f"التاريخ: {datetime.datetime.now().strftime('%Y-%m-%d')}"82 set_cell_rtl(right_cell)83 84 # =========================85 # إعداد الـ Footer86 # =========================87 footer = doc.sections[0].footer88 footer_paragraph = footer.paragraphs[0]89 90 footer_paragraph.text = "Powered by Camelson"91 footer_paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER92 93 for run in footer_paragraph.runs:94 run.font.size = Pt(10)95 96 set_rtl(footer_paragraph)97 98 99 lines = clean_text.split('\n')100 biometry_data = []101 102 for line in lines:103 line = line.strip()104 if not line:105 continue106 107 # استخراج قياسات البيومترية108 match = re.match(109 r"^(BPD|HC|AC|FL|CRL|GA|EDD|PAPP-A|NT|MSAFP)\s*[:|-]\s*(.*)",110 line,111 re.IGNORECASE112 )113 if match:114 biometry_data.append(match.groups())115 continue116 117 118 if line.startswith('#'):119 level = line.count('#')120 h = doc.add_heading(line.replace('#', '').strip(), level=min(level, 3))121 h.alignment = WD_ALIGN_PARAGRAPH.RIGHT122 set_rtl(h)123 124 elif line.startswith('**') and line.endswith('**'):125 h = doc.add_heading(line.replace('**', '').strip(), level=2)126 h.alignment = WD_ALIGN_PARAGRAPH.RIGHT127 set_rtl(h)128 129 130 elif line.startswith(('•', '-', '*')):131 p = doc.add_paragraph(line.strip('•-* '), style='List Bullet')132 p.alignment = WD_ALIGN_PARAGRAPH.RIGHT133 set_rtl(p)134 135 136 else:137 p = doc.add_paragraph(line)138 p.alignment = WD_ALIGN_PARAGRAPH.RIGHT139 set_rtl(p)140 141 if biometry_data:142 doc.add_page_break()143 144 summary_heading = doc.add_heading('جدول ملخص القياسات', level=2)145 summary_heading.alignment = WD_ALIGN_PARAGRAPH.RIGHT146 set_rtl(summary_heading)147 148 table = doc.add_table(rows=1, cols=2)149 table.style = 'Table Grid'150 table.autofit = True151 152 hdr_cells = table.rows[0].cells153 hdr_cells[0].text = 'نوع القياس'154 hdr_cells[1].text = 'القيمة / النتيجة السريرية'155 156 set_cell_rtl(hdr_cells[0])157 set_cell_rtl(hdr_cells[1])158 159 for name, value in biometry_data:160 row_cells = table.add_row().cells161 row_cells[0].text = name.upper()162 row_cells[1].text = value163 164 set_cell_rtl(row_cells[0])165 set_cell_rtl(row_cells[1])166 167 # حفظ الملف168 doc.save(output_path)