Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
@@ -1,69 +0,0 @@
|
|
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 |
-
# PDF to Word and Word to PDF converter
|
9 |
-
"""
|
10 |
-
|
11 |
-
def pdf_to_word(pdf_file):
|
12 |
-
docx_filename = pdf_file.name.replace('.pdf', '.docx')
|
13 |
-
|
14 |
-
cv = Converter(pdf_file.name)
|
15 |
-
cv.convert(docx_filename, multi_processing=True, start=0, end=None)
|
16 |
-
cv.close()
|
17 |
-
|
18 |
-
return docx_filename
|
19 |
-
|
20 |
-
def word_to_pdf(docx_file):
|
21 |
-
pdf_filename = "output.pdf"
|
22 |
-
|
23 |
-
doc = Document(docx_file)
|
24 |
-
pdf = FPDF(format='A4')
|
25 |
-
pdf.set_auto_page_break(auto=True, margin=15)
|
26 |
-
pdf.add_page()
|
27 |
-
pdf.add_font('Arial', '', 'Arial.ttf', uni=True)
|
28 |
-
pdf.set_font('Arial', size=12)
|
29 |
-
|
30 |
-
for para in doc.paragraphs:
|
31 |
-
text = para.text.strip()
|
32 |
-
if not text: # Ignorar linhas vazias
|
33 |
-
continue
|
34 |
-
# Quebrar o texto em várias linhas se necessário
|
35 |
-
words = text.split()
|
36 |
-
line = ''
|
37 |
-
for word in words:
|
38 |
-
if pdf.get_string_width(line + word) < (pdf.w - 2 * pdf.l_margin):
|
39 |
-
line += word + ' '
|
40 |
-
else:
|
41 |
-
pdf.cell(0, 10, line, ln=True)
|
42 |
-
line = word + ' '
|
43 |
-
if line:
|
44 |
-
pdf.cell(0, 10, line, ln=True)
|
45 |
-
|
46 |
-
pdf.output(pdf_filename)
|
47 |
-
return pdf_filename
|
48 |
-
|
49 |
-
with gr.Blocks() as app:
|
50 |
-
gr.Markdown(title_and_description)
|
51 |
-
|
52 |
-
with gr.Row():
|
53 |
-
with gr.Column():
|
54 |
-
with gr.Accordion("PDF to Word"):
|
55 |
-
pdf_input = gr.File(label="Upload PDF")
|
56 |
-
convert_pdf_to_word = gr.Button("Convert to Word")
|
57 |
-
word_output = gr.File(label="Download Word file", type="filepath")
|
58 |
-
|
59 |
-
convert_pdf_to_word.click(pdf_to_word, inputs=[pdf_input], outputs=[word_output])
|
60 |
-
|
61 |
-
with gr.Column():
|
62 |
-
with gr.Accordion("Word to PDF"):
|
63 |
-
word_input = gr.File(label="Upload Word")
|
64 |
-
convert_word_to_pdf = gr.Button("Convert to PDF")
|
65 |
-
pdf_output = gr.File(label="Download PDF file", type="filepath")
|
66 |
-
|
67 |
-
convert_word_to_pdf.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])
|
68 |
-
|
69 |
-
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|