alfanan1/t3del
0
1import streamlit as st2from docx import Document3from docx.shared import Inches, Pt4from docx.enum.text import WD_ALIGN_PARAGRAPH5import io6 7# --- المنطق البرمجي الخاص بك (مُعدل للعمل في الذاكرة) ---8 9def replace_text_everywhere(doc, old_text, new_text):10 for p in doc.paragraphs:11 if old_text in p.text:12 for run in p.runs:13 if old_text in run.text:14 run.text = run.text.replace(old_text, new_text)15 for table in doc.tables:16 for row in table.rows:17 for cell in row.cells:18 for p in cell.paragraphs:19 if old_text in p.text:20 p.text = p.text.replace(old_text, new_text)21 22def replace_tag_with_image(doc, tag, image_file):23 for p in doc.paragraphs:24 if tag in p.text:25 p.text = p.text.replace(tag, "")26 p.alignment = WD_ALIGN_PARAGRAPH.CENTER27 run = p.add_run()28 run.add_picture(image_file, width=Inches(4.5))29 return30 for table in doc.tables:31 for row in table.rows:32 for cell in row.cells:33 for p in cell.paragraphs:34 if tag in p.text:35 p.text = p.text.replace(tag, "")36 p.alignment = WD_ALIGN_PARAGRAPH.CENTER37 run = p.add_run()38 run.add_picture(image_file, width=Inches(4.5))39 return40 41# --- واجهة Streamlit ---42 43st.title("مطور المستندات ALFANAN 🏗️")44 45# 1. رفع القوالب46templates = st.file_uploader("ارفع قوالب الوورد الأربعة", accept_multiple_files=True, type=["docx"])47 48# 2. البيانات49name = st.text_input("اسم العميل")50address = st.text_input("العنوان")51notes = st.text_area("الملاحظات الفنية")52 53# 3. الإحداثيات54coords_list = []55for i in range(4):56 c1, c2 = st.columns(2)57 coords_list.append((c1.text_input(f"X{i+1}"), c2.text_input(f"Y{i+1}")))58 59# 4. الصور المرفقة60b_img = st.file_uploader("صورة المبنى", type=["jpg", "jpeg", "png"])61s_img = st.file_uploader("صورة الكروكي", type=["jpg", "jpeg", "png"])62c_img = st.file_uploader("صورة الإحداثيات", type=["jpg", "jpeg", "png"])63 64# --- المعالجة ---65if st.button("بدء المعالجة"):66 if len(templates) < 4:67 st.error("يرجى رفع 4 قوالب")68 else:69 for template in templates:70 doc = Document(template)71 72 # تنفيذ منطقك الأصلي73 replace_text_everywhere(doc, "{{CLIENT_NAME}}", name)74 replace_text_everywhere(doc, "{{CLIENT_ADDRESS}}", address)75 replace_text_everywhere(doc, "{{NOTES}}", notes)76 77 # الإحداثيات78 for i in range(4):79 replace_text_everywhere(doc, f"{{{{X{i+1}}}}}", coords_list[i][0])80 replace_text_everywhere(doc, f"{{{{Y{i+1}}}}}", coords_list[i][1])81 82 # الصور83 if b_img: replace_tag_with_image(doc, "{{BUILDING_IMAGE}}", b_img)84 if s_img: replace_tag_with_image(doc, "{{SKETCH_IMAGE}}", s_img)85 if c_img: replace_tag_with_image(doc, "{{COORDS_IMAGE}}", c_img)86 87 # حفظ وتحميل88 buffer = io.BytesIO()89 doc.save(buffer)90 buffer.seek(0)91 st.download_button(f"تحميل {template.name}", buffer, file_name=f"معدل_{template.name}")