File size: 1,548 Bytes
55a515e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 PyPDF2 import PdfFileReader, PdfFileWriter
import docx
import io

def pdf_to_word(pdf_file):
    pdfReader = PdfFileReader(pdf_file)
    doc = docx.Document()
    for page_num in range(pdfReader.numPages):
        page = pdfReader.getPage(page_num)
        text = page.extractText()
        doc.add_paragraph(text)
    
    doc_io = io.BytesIO()  # Criar um buffer de memória
    doc.save(doc_io)  # Salvar o documento no buffer
    doc_io.seek(0)  # Voltar ao início do buffer
    return doc_io

def word_to_pdf(docx_file):
    doc = docx.Document(docx_file)
    pdf_writer = PdfFileWriter()
    packet = io.BytesIO()

    for para in doc.paragraphs:
        packet.write(para.text.encode('utf-8'))

    pdf_writer.addBlankPage()
    packet.seek(0)
    return packet

with gr.Blocks() as demo:
    gr.Markdown("PDF <--> Word Converter")
    with gr.Row():
        with gr.Column():
            gr.Markdown("### PDF to Word")
            pdf_input = gr.File(label="Upload PDF")
            word_output = gr.File(label="Download Word", type="docx")
            pdf_to_word_btn = gr.Button("Convert")
        with gr.Column():
            gr.Markdown("### Word to PDF")
            word_input = gr.File(label="Upload Word")
            pdf_output = gr.File(label="Download PDF", type="pdf")
            word_to_pdf_btn = gr.Button("Convert")

    pdf_to_word_btn.click(pdf_to_word, inputs=[pdf_input], outputs=[word_output])
    word_to_pdf_btn.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])

demo.launch()