Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
def
|
| 5 |
if pdf_file is None:
|
| 6 |
-
return "يرجى رفع ملف PDF
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
with open(input_path, "wb") as f:
|
| 12 |
f.write(pdf_file.read())
|
| 13 |
|
| 14 |
try:
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
-
return f"حدث خطأ
|
| 21 |
|
| 22 |
-
# واجهة Gradio
|
| 23 |
demo = gr.Interface(
|
| 24 |
-
fn=
|
| 25 |
inputs=gr.File(label="📄 ارفع ملف PDF"),
|
| 26 |
-
outputs=gr.File(label="📥 تحميل
|
| 27 |
-
title="📝 محول PDF إلى Word",
|
| 28 |
-
description="
|
| 29 |
)
|
| 30 |
|
| 31 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import fitz # PyMuPDF
|
| 3 |
+
from docx import Document
|
| 4 |
+
from docx.shared import Inches
|
| 5 |
+
import os
|
| 6 |
+
import uuid
|
| 7 |
|
| 8 |
+
def pdf_to_word(pdf_file):
|
| 9 |
if pdf_file is None:
|
| 10 |
+
return "يرجى رفع ملف PDF."
|
| 11 |
|
| 12 |
+
pdf_path = f"{uuid.uuid4()}.pdf"
|
| 13 |
+
docx_path = f"{uuid.uuid4()}.docx"
|
| 14 |
+
with open(pdf_path, "wb") as f:
|
|
|
|
| 15 |
f.write(pdf_file.read())
|
| 16 |
|
| 17 |
try:
|
| 18 |
+
# فتح PDF
|
| 19 |
+
doc = fitz.open(pdf_path)
|
| 20 |
+
document = Document()
|
| 21 |
+
|
| 22 |
+
for page_number in range(len(doc)):
|
| 23 |
+
page = doc.load_page(page_number)
|
| 24 |
+
pix = page.get_pixmap(dpi=150)
|
| 25 |
+
image_path = f"page_{page_number}.png"
|
| 26 |
+
pix.save(image_path)
|
| 27 |
+
|
| 28 |
+
document.add_paragraph(f"صفحة {page_number + 1}")
|
| 29 |
+
document.add_picture(image_path, width=Inches(6))
|
| 30 |
+
os.remove(image_path)
|
| 31 |
+
|
| 32 |
+
document.save(docx_path)
|
| 33 |
+
os.remove(pdf_path)
|
| 34 |
+
return docx_path
|
| 35 |
+
|
| 36 |
except Exception as e:
|
| 37 |
+
return f"حدث خطأ: {str(e)}"
|
| 38 |
|
|
|
|
| 39 |
demo = gr.Interface(
|
| 40 |
+
fn=pdf_to_word,
|
| 41 |
inputs=gr.File(label="📄 ارفع ملف PDF"),
|
| 42 |
+
outputs=gr.File(label="📥 تحميل Word الناتج"),
|
| 43 |
+
title="📝 محول PDF إلى Word (صوري)",
|
| 44 |
+
description="هذا المحول يقوم بتحويل كل صفحة في PDF إلى صورة ثم يضعها داخل ملف Word. مناسب للوثائق البسيطة والملاحظات."
|
| 45 |
)
|
| 46 |
|
| 47 |
demo.launch()
|