Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,50 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from docx import Document
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def
|
| 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 |
-
escritor = converter_docx_para_pdf(caminho)
|
| 50 |
-
formato = "pdf"
|
| 51 |
-
salvar_arquivo(escritor, formato, nome)
|
| 52 |
-
|
| 53 |
-
# Interface
|
| 54 |
-
interface = gr.Interface(fn=converter, inputs=[caminho_arquivo, opcoes_conversao], outputs=[nome_arquivo], title="Conversor de PDF/Word", theme="default")
|
| 55 |
-
|
| 56 |
-
# Exibir a interface
|
| 57 |
-
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from pdf2docx import Converter
|
| 3 |
+
from fpdf import FPDF
|
| 4 |
from docx import Document
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def pdf_to_word(pdf_file):
|
| 8 |
+
docx_filename = pdf_file.name.replace('.pdf', '.docx')
|
| 9 |
+
|
| 10 |
+
cv = Converter(pdf_file.name)
|
| 11 |
+
cv.convert(docx_filename, start=0, end=None)
|
| 12 |
+
cv.close()
|
| 13 |
+
|
| 14 |
+
return docx_filename
|
| 15 |
+
|
| 16 |
+
def word_to_pdf(docx_file):
|
| 17 |
+
pdf_filename = docx_file.name.replace('.docx', '.pdf')
|
| 18 |
+
|
| 19 |
+
document = Document(docx_file.name)
|
| 20 |
+
pdf = FPDF()
|
| 21 |
+
|
| 22 |
+
pdf.add_page()
|
| 23 |
+
|
| 24 |
+
for paragraph in document.paragraphs:
|
| 25 |
+
pdf.set_font("Arial", size = 12)
|
| 26 |
+
pdf.multi_cell(0, 10, paragraph.text)
|
| 27 |
+
|
| 28 |
+
pdf.output(pdf_filename)
|
| 29 |
+
|
| 30 |
+
return pdf_filename
|
| 31 |
+
|
| 32 |
+
with gr.Blocks() as app:
|
| 33 |
+
with gr.Row():
|
| 34 |
+
with gr.Column():
|
| 35 |
+
with gr.Accordion("PDF to Word"):
|
| 36 |
+
pdf_input = gr.File(label="Upload PDF")
|
| 37 |
+
convert_pdf_to_word = gr.Button("Convert to Word")
|
| 38 |
+
word_output = gr.File(label="Download Word File", type="file")
|
| 39 |
+
|
| 40 |
+
convert_pdf_to_word.click(pdf_to_word, inputs=[pdf_input], outputs=[word_output])
|
| 41 |
+
|
| 42 |
+
with gr.Column():
|
| 43 |
+
with gr.Accordion("Word to PDF"):
|
| 44 |
+
word_input = gr.File(label="Upload Word")
|
| 45 |
+
convert_word_to_pdf = gr.Button("Convert to PDF")
|
| 46 |
+
pdf_output = gr.File(label="Download PDF File", type="file")
|
| 47 |
+
|
| 48 |
+
convert_word_to_pdf.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])
|
| 49 |
+
|
| 50 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|