File size: 1,548 Bytes
55a515e
9e7436f
 
5d9624d
9e7436f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from pdf2docx import Converter
from fpdf import FPDF
from docx import Document
import os

def pdf_to_word(pdf_file):
    docx_filename = pdf_file.name.replace('.pdf', '.docx')
    
    cv = Converter(pdf_file.name)
    cv.convert(docx_filename, start=0, end=None)
    cv.close()
    
    return docx_filename

def word_to_pdf(docx_file):
    pdf_filename = docx_file.name.replace('.docx', '.pdf')
    
    document = Document(docx_file.name)
    pdf = FPDF()

    pdf.add_page()
    
    for paragraph in document.paragraphs:
        pdf.set_font("Arial", size = 12)
        pdf.multi_cell(0, 10, paragraph.text)
    
    pdf.output(pdf_filename)
    
    return pdf_filename

with gr.Blocks() as app:
    with gr.Row():
        with gr.Column():
            with gr.Accordion("PDF to Word"):
                pdf_input = gr.File(label="Upload PDF")
                convert_pdf_to_word = gr.Button("Convert to Word")
                word_output = gr.File(label="Download Word File", type="file")
                
                convert_pdf_to_word.click(pdf_to_word, inputs=[pdf_input], outputs=[word_output])
                
        with gr.Column():
            with gr.Accordion("Word to PDF"):
                word_input = gr.File(label="Upload Word")
                convert_word_to_pdf = gr.Button("Convert to PDF")
                pdf_output = gr.File(label="Download PDF File", type="file")
                
                convert_word_to_pdf.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])

app.launch()