Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pdf2docx import Converter
|
| 3 |
+
from docx import Document
|
| 4 |
+
from fpdf import FPDF
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
title_and_description = """
|
| 8 |
+
# Conversor de PDF a Word y Word a PDF
|
| 9 |
+
Creado por [@artificialguybr](https://artificialguy.com)
|
| 10 |
+
|
| 11 |
+
Sube un archivo PDF para convertirlo a Word o un archivo Word para convertirlo a PDF.
|
| 12 |
+
|
| 13 |
+
## Características
|
| 14 |
+
- **Fácil de usar**: Interfaz sencilla para subir archivos PDF o Word y convertirlos al formato deseado.
|
| 15 |
+
- **Alta calidad**: Convierte manteniendo la mejor calidad posible.
|
| 16 |
+
- **Procesamiento eficiente**: Utiliza `pdf2docx`, `fpdf` y `docx` para conversiones rápidas y confiables.
|
| 17 |
+
- **Uso ilimitado**: Sin límite de archivos. ¡Úsalo sin restricciones!
|
| 18 |
+
|
| 19 |
+
¡Siéntete libre de usarlo en tus propios documentos!
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
def pdf_to_word(pdf_file):
|
| 23 |
+
docx_filename = pdf_file.name.replace('.pdf', '.docx')
|
| 24 |
+
|
| 25 |
+
cv = Converter(pdf_file.name)
|
| 26 |
+
cv.convert(docx_filename, multi_processing=True, start=0, end=None)
|
| 27 |
+
cv.close()
|
| 28 |
+
|
| 29 |
+
return docx_filename
|
| 30 |
+
|
| 31 |
+
def word_to_pdf(docx_file):
|
| 32 |
+
pdf_filename = "output.pdf"
|
| 33 |
+
|
| 34 |
+
doc = Document(docx_file)
|
| 35 |
+
pdf = FPDF(format='A4')
|
| 36 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 37 |
+
pdf.add_page()
|
| 38 |
+
pdf.add_font('Arial', '', 'Arial.ttf', uni=True)
|
| 39 |
+
pdf.set_font('Arial', size=12)
|
| 40 |
+
|
| 41 |
+
for para in doc.paragraphs:
|
| 42 |
+
text = para.text.strip()
|
| 43 |
+
if not text: # Ignorar linhas vazias
|
| 44 |
+
continue
|
| 45 |
+
# Quebrar o texto em várias linhas se necessário
|
| 46 |
+
words = text.split()
|
| 47 |
+
line = ''
|
| 48 |
+
for word in words:
|
| 49 |
+
if pdf.get_string_width(line + word) < (pdf.w - 2 * pdf.l_margin):
|
| 50 |
+
line += word + ' '
|
| 51 |
+
else:
|
| 52 |
+
pdf.cell(0, 10, line, ln=True)
|
| 53 |
+
line = word + ' '
|
| 54 |
+
if line:
|
| 55 |
+
pdf.cell(0, 10, line, ln=True)
|
| 56 |
+
|
| 57 |
+
pdf.output(pdf_filename)
|
| 58 |
+
return pdf_filename
|
| 59 |
+
|
| 60 |
+
with gr.Blocks() as app:
|
| 61 |
+
gr.Markdown(title_and_description)
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
with gr.Column():
|
| 65 |
+
with gr.Accordion("PDF a Word"):
|
| 66 |
+
pdf_input = gr.File(label="Subir PDF")
|
| 67 |
+
convert_pdf_to_word = gr.Button("Convertir a Word")
|
| 68 |
+
word_output = gr.File(label="Descargar archivo Word", type="filepath")
|
| 69 |
+
|
| 70 |
+
convert_pdf_to_word.click(pdf_to_word, inputs=[pdf_input], outputs=[word_output])
|
| 71 |
+
|
| 72 |
+
with gr.Column():
|
| 73 |
+
with gr.Accordion("Word a PDF"):
|
| 74 |
+
word_input = gr.File(label="Subir Word")
|
| 75 |
+
convert_word_to_pdf = gr.Button("Convertir a PDF")
|
| 76 |
+
pdf_output = gr.File(label="Descargar archivo PDF", type="filepath")
|
| 77 |
+
|
| 78 |
+
convert_word_to_pdf.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])
|
| 79 |
+
|
| 80 |
+
app.launch()
|