Update app.py
Browse files
app.py
CHANGED
|
@@ -6,42 +6,41 @@ import os
|
|
| 6 |
import uuid
|
| 7 |
import traceback
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
if pdf_file is None:
|
| 11 |
-
return "ูุฑุฌู ุฑูุน ู
ูู PDF."
|
| 12 |
-
|
| 13 |
try:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
with open(
|
| 17 |
f.write(pdf_file.read())
|
| 18 |
|
| 19 |
-
# ูุชุญ PDF
|
| 20 |
-
doc = fitz.open(
|
| 21 |
document = Document()
|
| 22 |
-
|
| 23 |
-
for
|
| 24 |
-
page = doc.load_page(
|
| 25 |
pix = page.get_pixmap(dpi=150)
|
| 26 |
-
|
| 27 |
-
pix.save(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return docx_path
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
-
return traceback.format_exc()
|
| 39 |
|
| 40 |
-
# ูุฐุง ูู ุงูุณุทุฑ ุงูู
ูู
: ูุฌุจ ุฃู ูููู ุงุณู
ุงูู
ุชุบูุฑ app
|
| 41 |
app = gr.Interface(
|
| 42 |
-
fn=
|
| 43 |
-
inputs=gr.File(label="
|
| 44 |
-
outputs=gr.File(label="๐ฅ ุชุญู
ูู Word
|
| 45 |
-
title="
|
| 46 |
-
description="
|
| 47 |
)
|
|
|
|
| 6 |
import uuid
|
| 7 |
import traceback
|
| 8 |
|
| 9 |
+
def convert_pdf_to_word(pdf_file):
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
+
# ุญูุธ ุงูู
ูู ุงูู
ุฑููุน
|
| 12 |
+
temp_pdf_path = f"{uuid.uuid4()}.pdf"
|
| 13 |
+
with open(temp_pdf_path, "wb") as f:
|
| 14 |
f.write(pdf_file.read())
|
| 15 |
|
| 16 |
+
# ูุชุญ ู
ูู PDF
|
| 17 |
+
doc = fitz.open(temp_pdf_path)
|
| 18 |
document = Document()
|
| 19 |
+
|
| 20 |
+
for i in range(len(doc)):
|
| 21 |
+
page = doc.load_page(i)
|
| 22 |
pix = page.get_pixmap(dpi=150)
|
| 23 |
+
img_path = f"page_{i}.png"
|
| 24 |
+
pix.save(img_path)
|
| 25 |
+
|
| 26 |
+
document.add_paragraph(f"๐ ุตูุญุฉ {i+1}")
|
| 27 |
+
document.add_picture(img_path, width=Inches(6))
|
| 28 |
+
os.remove(img_path)
|
| 29 |
|
| 30 |
+
# ุญูุธ ุงูู
ูู ุงูููุงุฆู
|
| 31 |
+
output_path = f"{uuid.uuid4()}.docx"
|
| 32 |
+
document.save(output_path)
|
| 33 |
|
| 34 |
+
os.remove(temp_pdf_path)
|
| 35 |
+
return output_path
|
|
|
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
+
return f"โ ุฎุทุฃ: \n{traceback.format_exc()}"
|
| 39 |
|
|
|
|
| 40 |
app = gr.Interface(
|
| 41 |
+
fn=convert_pdf_to_word,
|
| 42 |
+
inputs=gr.File(label="๐ค ุงุฑูุน ู
ูู PDF", type="file"),
|
| 43 |
+
outputs=gr.File(label="๐ฅ ุชุญู
ูู Word"),
|
| 44 |
+
title="๐ ู
ุญูู PDF ุฅูู Word (ุจุงูุตูุฑ)",
|
| 45 |
+
description="ุญูู ุตูุญุงุช PDF ุฅูู ุตูุฑ ูุงุฏู
ุฌูุง ุชููุงุฆููุง ุฏุงุฎู ู
ุณุชูุฏ Word. ู
ูุงุณุจ ููุนุฑุถ ูุงูุทุจุงุนุฉ."
|
| 46 |
)
|