Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,70 @@
|
|
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
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
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 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
with gr.Column():
|
73 |
-
with gr.Accordion("Word to PDF"):
|
74 |
-
word_input = gr.File(label="Upload Word")
|
75 |
-
convert_word_to_pdf = gr.Button("Convert to PDF")
|
76 |
-
pdf_output = gr.File(label="Download PDF file", type="filepath")
|
77 |
-
|
78 |
-
convert_word_to_pdf.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])
|
79 |
-
|
80 |
-
app.launch()
|
|
|
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
|
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 |
+
|
31 |
+
for para in doc.paragraphs:
|
32 |
+
text = para.text.strip()
|
33 |
+
if not text:
|
34 |
+
continue
|
35 |
+
|
36 |
+
words = text.split()
|
37 |
+
line = ''
|
38 |
+
for word in words:
|
39 |
+
if pdf.get_string_width(line + word) < (pdf.w - 2 * pdf.l_margin):
|
40 |
+
line += word + ' '
|
41 |
+
else:
|
42 |
+
pdf.cell(0, 10, line, ln=True)
|
43 |
+
line = word + ' '
|
44 |
+
if line:
|
45 |
+
pdf.cell(0, 10, line, ln=True)
|
46 |
+
|
47 |
+
pdf.output(pdf_filename)
|
48 |
+
return pdf_filename
|
49 |
+
|
50 |
+
with gr.Blocks() as app:
|
51 |
+
gr.Markdown(title_and_description)
|
52 |
+
|
53 |
+
with gr.Row():
|
54 |
+
with gr.Column():
|
55 |
+
with gr.Accordion("PDF to Word"):
|
56 |
+
pdf_input = gr.File(label="Upload PDF")
|
57 |
+
convert_pdf_to_word = gr.Button("Convert to Word")
|
58 |
+
word_output = gr.File(label="Download Word file", type="filepath")
|
59 |
+
|
60 |
+
convert_pdf_to_word.click(pdf_to_word, inputs=[pdf_input], outputs=[word_output])
|
61 |
+
|
62 |
+
with gr.Column():
|
63 |
+
with gr.Accordion("Word to PDF"):
|
64 |
+
word_input = gr.File(label="Upload Word")
|
65 |
+
convert_word_to_pdf = gr.Button("Convert to PDF")
|
66 |
+
pdf_output = gr.File(label="Download PDF file", type="filepath")
|
67 |
+
|
68 |
+
convert_word_to_pdf.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])
|
69 |
+
|
70 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|