Spaces:
Sleeping
Sleeping
import gradio as gr | |
from pdf2docx import Converter | |
from docx import Document | |
from fpdf import FPDF | |
import os | |
def pdf_para_word(arquivo_pdf): | |
nome_docx = arquivo_pdf.name.replace('.pdf', '.docx') | |
cv = Converter(arquivo_pdf.name) | |
cv.convert(nome_docx, multi_processing=True, start=0, end=None) | |
cv.close() | |
return nome_docx | |
def word_para_pdf(arquivo_docx): | |
nome_pdf = "output.pdf" | |
doc = Document(arquivo_docx) | |
pdf = FPDF(format='A4') | |
pdf.set_auto_page_break(auto=True, margin=15) | |
pdf.add_page() | |
pdf.add_font('Arial', '', 'Arial.ttf', uni=True) | |
pdf.set_font('Arial', size=12) | |
for para in doc.paragraphs: | |
texto = para.text.strip() | |
if not texto: # Ignorar linhas vazias | |
continue | |
# Quebrar o texto em várias linhas se necessário | |
palavras = texto.split() | |
linha = '' | |
for palavra in palavras: | |
if pdf.get_string_width(linha + palavra) < (pdf.w - 2 * pdf.l_margin): | |
linha += palavra + ' ' | |
else: | |
pdf.cell(0, 10, linha, ln=True) | |
linha = palavra + ' ' | |
if linha: | |
pdf.cell(0, 10, linha, ln=True) | |
pdf.output(nome_pdf) | |
return nome_pdf | |
with gr.Blocks() as app: | |
with gr.Row(): | |
with gr.Column(): | |
with gr.Accordion("PDF para Word"): | |
entrada_pdf = gr.File(label="Enviar PDF") | |
converter_pdf_para_word = gr.Button("Converter para Word") | |
saida_word = gr.File(label="Baixar arquivo Word", type="filepath") | |
converter_pdf_para_word.click(pdf_para_word, inputs=[entrada_pdf], outputs=[saida_word]) | |
with gr.Column(): | |
with gr.Accordion("Word para PDF"): | |
entrada_word = gr.File(label="Enviar Word") | |
converter_word_para_pdf = gr.Button("Converter para PDF") | |
saida_pdf = gr.File(label="Baixar arquivo PDF", type="filepath") | |
converter_word_para_pdf.click(word_para_pdf, inputs=[entrada_word], outputs=[saida_pdf]) | |
app.launch() | |