Spaces:
Runtime error
Runtime error
artificialguybr
commited on
Commit
•
55a515e
1
Parent(s):
2ea6290
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PyPDF2 import PdfFileReader, PdfFileWriter
|
3 |
+
import docx
|
4 |
+
import io
|
5 |
+
|
6 |
+
def pdf_to_word(pdf_file):
|
7 |
+
pdfReader = PdfFileReader(pdf_file)
|
8 |
+
doc = docx.Document()
|
9 |
+
for page_num in range(pdfReader.numPages):
|
10 |
+
page = pdfReader.getPage(page_num)
|
11 |
+
text = page.extractText()
|
12 |
+
doc.add_paragraph(text)
|
13 |
+
|
14 |
+
doc_io = io.BytesIO() # Criar um buffer de memória
|
15 |
+
doc.save(doc_io) # Salvar o documento no buffer
|
16 |
+
doc_io.seek(0) # Voltar ao início do buffer
|
17 |
+
return doc_io
|
18 |
+
|
19 |
+
def word_to_pdf(docx_file):
|
20 |
+
doc = docx.Document(docx_file)
|
21 |
+
pdf_writer = PdfFileWriter()
|
22 |
+
packet = io.BytesIO()
|
23 |
+
|
24 |
+
for para in doc.paragraphs:
|
25 |
+
packet.write(para.text.encode('utf-8'))
|
26 |
+
|
27 |
+
pdf_writer.addBlankPage()
|
28 |
+
packet.seek(0)
|
29 |
+
return packet
|
30 |
+
|
31 |
+
with gr.Blocks() as demo:
|
32 |
+
gr.Markdown("PDF <--> Word Converter")
|
33 |
+
with gr.Row():
|
34 |
+
with gr.Column():
|
35 |
+
gr.Markdown("### PDF to Word")
|
36 |
+
pdf_input = gr.File(label="Upload PDF")
|
37 |
+
word_output = gr.File(label="Download Word", type="docx")
|
38 |
+
pdf_to_word_btn = gr.Button("Convert")
|
39 |
+
with gr.Column():
|
40 |
+
gr.Markdown("### Word to PDF")
|
41 |
+
word_input = gr.File(label="Upload Word")
|
42 |
+
pdf_output = gr.File(label="Download PDF", type="pdf")
|
43 |
+
word_to_pdf_btn = gr.Button("Convert")
|
44 |
+
|
45 |
+
pdf_to_word_btn.click(pdf_to_word, inputs=[pdf_input], outputs=[word_output])
|
46 |
+
word_to_pdf_btn.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])
|
47 |
+
|
48 |
+
demo.launch()
|
49 |
+
|