File size: 1,557 Bytes
1c68fff
 
 
 
81f7e9a
1c68fff
9c08771
1c68fff
 
81f7e9a
 
 
 
1c68fff
9c08771
1c68fff
81f7e9a
 
 
 
 
 
 
 
1c68fff
81f7e9a
 
 
 
1c68fff
81f7e9a
 
1c68fff
 
81f7e9a
1c68fff
 
81f7e9a
9c08771
 
1c68fff
 
9c08771
 
1c68fff
 
 
81f7e9a
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
import gradio as gr
import subprocess
import tempfile
from pathlib import Path
import time

def convert_docx_to_jpg(docx_file):
    try:
        with tempfile.TemporaryDirectory() as tmpdir:
            tmpdir = Path(tmpdir)
            
            # 1. Save the uploaded file
            docx_path = tmpdir / "input.docx"
            with open(docx_path, "wb") as f:
                f.write(open(docx_file.name, "rb").read())
            
            # 2. Convert DOCX to JPG using LibreOffice's export function
            subprocess.run([
                "soffice",
                "--headless",
                "--convert-to", "jpg",
                "--outdir", str(tmpdir),
                str(docx_path)
            ], check=True, timeout=30)
            
            # 3. Find the output file (LibreOffice adds '_1' sometimes)
            jpg_file = next(tmpdir.glob("*.jpg"), None)
            if not jpg_file:
                raise ValueError("No JPG file was created")
            
            # 4. Return the first page only
            return str(jpg_file)
            
    except Exception as e:
        raise gr.Error(f"Conversion failed. Try a simpler DOCX file. Error: {str(e)}")

with gr.Blocks() as app:
    gr.Markdown("## πŸ“„ DOCX to JPG (Working Version)")
    file_input = gr.File(label="Upload DOCX", file_types=[".docx"])
    output_image = gr.Image(label="Output JPG", type="filepath")
    
    gr.Button("Convert").click(
        fn=convert_docx_to_jpg,
        inputs=file_input,
        outputs=output_image
    )

app.launch()