Spaces:
Runtime error
Runtime error
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() | |