File size: 2,026 Bytes
04cb688
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import gradio as gr
import subprocess
import tempfile
import os
from pdf2image import convert_from_path
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def verify_libreoffice():
    """Check if LibreOffice exists before conversion"""
    try:
        result = subprocess.run(
            ["/usr/bin/libreoffice", "--version"],
            check=True,
            capture_output=True,
            text=True
        )
        logger.info(f"LibreOffice verified: {result.stdout.strip()}")
        return True
    except Exception as e:
        logger.error(f"LibreOffice check failed: {str(e)}")
        return False

def convert_docx_to_image(docx_path):
    if not verify_libreoffice():
        raise gr.Error("System Error: LibreOffice not installed!")
    
    with tempfile.TemporaryDirectory() as tmpdir:
        try:
            # Conversion steps...
            pdf_path = os.path.join(tmpdir, "output.pdf")
            subprocess.run([
                "/usr/bin/libreoffice",
                "--headless",
                "--convert-to", "pdf",
                "--outdir", tmpdir,
                docx_path
            ], check=True, timeout=30)
            
            images = convert_from_path(
                pdf_path,
                dpi=100,
                poppler_path="/usr/bin"
            )
            return images[0] if images else None
            
        except Exception as e:
            logger.error(f"Conversion error: {str(e)}")
            raise gr.Error(f"Failed: {str(e)}")

# Gradio Interface
with gr.Blocks() as app:
    gr.Markdown("## 🖥️ DOCX to Image Converter")
    with gr.Row():
        input_file = gr.File(
            label="Upload DOCX",
            file_types=[".docx"],
            type="filepath"
        )
        output_image = gr.Image(label="Output Image")
    
    gr.Button("Convert").click(
        fn=convert_docx_to_image,
        inputs=input_file,
        outputs=output_image
    )

app.launch()