Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,34 +4,19 @@ from docx import Document
|
|
4 |
from fpdf import FPDF
|
5 |
import os
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
Created by [@artificialguybr](https://artificialguy.com)
|
10 |
-
|
11 |
-
Upload a PDF file to convert to Word or a Word file to convert to PDF.
|
12 |
-
|
13 |
-
## Features
|
14 |
-
- **Easy to use**: Simple interface to upload PDF or Word files and convert to the desired format.
|
15 |
-
- **High quality**: Converts while maintaining the best possible quality.
|
16 |
-
- **Efficient processing**: Uses `pdf2docx`, `fpdf` and `docx` for fast and reliable conversions.
|
17 |
-
- **Unlimited Use**: No file limit. Use unlimited!
|
18 |
-
|
19 |
-
Feel free to use in your own documents!
|
20 |
-
"""
|
21 |
-
|
22 |
-
def pdf_to_word(pdf_file):
|
23 |
-
docx_filename = pdf_file.name.replace('.pdf', '.docx')
|
24 |
|
25 |
-
cv = Converter(
|
26 |
-
cv.convert(
|
27 |
cv.close()
|
28 |
|
29 |
-
return
|
30 |
|
31 |
-
def
|
32 |
-
|
33 |
|
34 |
-
doc = Document(
|
35 |
pdf = FPDF(format='A4')
|
36 |
pdf.set_auto_page_break(auto=True, margin=15)
|
37 |
pdf.add_page()
|
@@ -39,42 +24,40 @@ def word_to_pdf(docx_file):
|
|
39 |
pdf.set_font('Arial', size=12)
|
40 |
|
41 |
for para in doc.paragraphs:
|
42 |
-
|
43 |
-
if not
|
44 |
continue
|
45 |
# Quebrar o texto em várias linhas se necessário
|
46 |
-
|
47 |
-
|
48 |
-
for
|
49 |
-
if pdf.get_string_width(
|
50 |
-
|
51 |
else:
|
52 |
-
pdf.cell(0, 10,
|
53 |
-
|
54 |
-
if
|
55 |
-
pdf.cell(0, 10,
|
56 |
|
57 |
-
pdf.output(
|
58 |
-
return
|
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
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
|
70 |
-
|
71 |
|
72 |
with gr.Column():
|
73 |
-
with gr.Accordion("Word
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
|
78 |
-
|
79 |
|
80 |
app.launch()
|
|
|
4 |
from fpdf import FPDF
|
5 |
import os
|
6 |
|
7 |
+
def pdf_para_word(arquivo_pdf):
|
8 |
+
nome_docx = arquivo_pdf.name.replace('.pdf', '.docx')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
cv = Converter(arquivo_pdf.name)
|
11 |
+
cv.convert(nome_docx, multi_processing=True, start=0, end=None)
|
12 |
cv.close()
|
13 |
|
14 |
+
return nome_docx
|
15 |
|
16 |
+
def word_para_pdf(arquivo_docx):
|
17 |
+
nome_pdf = "output.pdf"
|
18 |
|
19 |
+
doc = Document(arquivo_docx)
|
20 |
pdf = FPDF(format='A4')
|
21 |
pdf.set_auto_page_break(auto=True, margin=15)
|
22 |
pdf.add_page()
|
|
|
24 |
pdf.set_font('Arial', size=12)
|
25 |
|
26 |
for para in doc.paragraphs:
|
27 |
+
texto = para.text.strip()
|
28 |
+
if not texto: # Ignorar linhas vazias
|
29 |
continue
|
30 |
# Quebrar o texto em várias linhas se necessário
|
31 |
+
palavras = texto.split()
|
32 |
+
linha = ''
|
33 |
+
for palavra in palavras:
|
34 |
+
if pdf.get_string_width(linha + palavra) < (pdf.w - 2 * pdf.l_margin):
|
35 |
+
linha += palavra + ' '
|
36 |
else:
|
37 |
+
pdf.cell(0, 10, linha, ln=True)
|
38 |
+
linha = palavra + ' '
|
39 |
+
if linha:
|
40 |
+
pdf.cell(0, 10, linha, ln=True)
|
41 |
|
42 |
+
pdf.output(nome_pdf)
|
43 |
+
return nome_pdf
|
44 |
|
45 |
with gr.Blocks() as app:
|
|
|
|
|
46 |
with gr.Row():
|
47 |
with gr.Column():
|
48 |
+
with gr.Accordion("PDF para Word"):
|
49 |
+
entrada_pdf = gr.File(label="Enviar PDF")
|
50 |
+
converter_pdf_para_word = gr.Button("Converter para Word")
|
51 |
+
saida_word = gr.File(label="Baixar arquivo Word", type="filepath")
|
52 |
|
53 |
+
converter_pdf_para_word.click(pdf_para_word, inputs=[entrada_pdf], outputs=[saida_word])
|
54 |
|
55 |
with gr.Column():
|
56 |
+
with gr.Accordion("Word para PDF"):
|
57 |
+
entrada_word = gr.File(label="Enviar Word")
|
58 |
+
converter_word_para_pdf = gr.Button("Converter para PDF")
|
59 |
+
saida_pdf = gr.File(label="Baixar arquivo PDF", type="filepath")
|
60 |
|
61 |
+
converter_word_para_pdf.click(word_para_pdf, inputs=[entrada_word], outputs=[saida_pdf])
|
62 |
|
63 |
app.launch()
|