|
import gradio as gr |
|
import fitz |
|
from docx import Document |
|
from docx.shared import Inches |
|
import os |
|
import uuid |
|
import traceback |
|
|
|
def convert_pdf_to_word(pdf_bytes): |
|
try: |
|
|
|
temp_pdf_path = f"{uuid.uuid4()}.pdf" |
|
with open(temp_pdf_path, "wb") as f: |
|
f.write(pdf_bytes) |
|
|
|
|
|
doc = fitz.open(temp_pdf_path) |
|
document = Document() |
|
|
|
for i in range(len(doc)): |
|
page = doc.load_page(i) |
|
pix = page.get_pixmap(dpi=150) |
|
img_path = f"page_{i}.png" |
|
pix.save(img_path) |
|
|
|
document.add_paragraph(f"๐ ุตูุญุฉ {i+1}") |
|
document.add_picture(img_path, width=Inches(6)) |
|
os.remove(img_path) |
|
|
|
|
|
output_path = f"{uuid.uuid4()}.docx" |
|
document.save(output_path) |
|
|
|
os.remove(temp_pdf_path) |
|
return output_path |
|
|
|
except Exception as e: |
|
return f"โ ุฎุทุฃ: \n{traceback.format_exc()}" |
|
|
|
app = gr.Interface( |
|
fn=convert_pdf_to_word, |
|
inputs=gr.File(label="๐ค ุงุฑูุน ู
ูู PDF", type="binary"), |
|
outputs=gr.File(label="๐ฅ ุชุญู
ูู Word"), |
|
title="๐ ู
ุญูู PDF ุฅูู Word (ุจุงูุตูุฑ)", |
|
description="ุญูู ุตูุญุงุช PDF ุฅูู ุตูุฑ ูุงุฏู
ุฌูุง ุชููุงุฆููุง ุฏุงุฎู ู
ุณุชูุฏ Word. ู
ูุงุณุจ ููุนุฑุถ ูุงูุทุจุงุนุฉ." |
|
) |
|
|