Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,48 +3,72 @@ 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 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def word_to_pdf(docx_file):
|
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 |
with gr.Blocks() as app:
|
50 |
gr.Markdown(title_and_description)
|
@@ -52,18 +76,18 @@ with gr.Blocks() as app:
|
|
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()
|
|
|
3 |
from docx import Document
|
4 |
from fpdf import FPDF
|
5 |
import os
|
6 |
+
import tempfile
|
7 |
|
8 |
title_and_description = """
|
9 |
+
# PDF to Word and Word to PDF Converter
|
10 |
+
|
11 |
+
This tool allows you to convert PDF files to Word documents and Word documents to PDF files.
|
12 |
"""
|
13 |
|
14 |
def pdf_to_word(pdf_file):
|
15 |
+
"""
|
16 |
+
Converts a PDF file to a Word document.
|
17 |
+
"""
|
18 |
+
try:
|
19 |
+
# Create a temporary directory to store the output file
|
20 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
21 |
+
docx_filename = os.path.join(temp_dir, os.path.basename(pdf_file.name).replace('.pdf', '.docx'))
|
22 |
+
|
23 |
+
# Convert PDF to Word
|
24 |
+
cv = Converter(pdf_file.name)
|
25 |
+
cv.convert(docx_filename, multi_processing=True, start=0, end=None)
|
26 |
+
cv.close()
|
27 |
+
|
28 |
+
# Return the path to the converted file
|
29 |
+
return docx_filename
|
30 |
+
except Exception as e:
|
31 |
+
return f"Error: {e}"
|
32 |
|
33 |
def word_to_pdf(docx_file):
|
34 |
+
"""
|
35 |
+
Converts a Word document to a PDF file.
|
36 |
+
"""
|
37 |
+
try:
|
38 |
+
# Create a temporary directory to store the output file
|
39 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
40 |
+
pdf_filename = os.path.join(temp_dir, "output.pdf")
|
41 |
+
|
42 |
+
# Convert Word to PDF
|
43 |
+
doc = Document(docx_file.name)
|
44 |
+
pdf = FPDF(format='A4')
|
45 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
46 |
+
pdf.add_page()
|
47 |
+
pdf.add_font('Arial', '', 'Arial.ttf', uni=True)
|
48 |
+
pdf.set_font('Arial', size=12)
|
49 |
|
50 |
+
for para in doc.paragraphs:
|
51 |
+
text = para.text.strip()
|
52 |
+
if not text:
|
53 |
+
continue
|
54 |
|
55 |
+
words = text.split()
|
56 |
+
line = ''
|
57 |
+
for word in words:
|
58 |
+
if pdf.get_string_width(line + word) < (pdf.w - 2 * pdf.l_margin):
|
59 |
+
line += word + ' '
|
60 |
+
else:
|
61 |
+
pdf.cell(0, 10, line, ln=True)
|
62 |
+
line = word + ' '
|
63 |
+
if line:
|
64 |
+
pdf.cell(0, 10, line, ln=True)
|
65 |
|
66 |
+
pdf.output(pdf_filename)
|
67 |
+
|
68 |
+
# Return the path to the converted file
|
69 |
+
return pdf_filename
|
70 |
+
except Exception as e:
|
71 |
+
return f"Error: {e}"
|
72 |
|
73 |
with gr.Blocks() as app:
|
74 |
gr.Markdown(title_and_description)
|
|
|
76 |
with gr.Row():
|
77 |
with gr.Column():
|
78 |
with gr.Accordion("PDF to Word"):
|
79 |
+
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
80 |
convert_pdf_to_word = gr.Button("Convert to Word")
|
81 |
+
word_output = gr.File(label="Download Word file", type="filepath", file_types=[".docx"])
|
82 |
|
83 |
convert_pdf_to_word.click(pdf_to_word, inputs=[pdf_input], outputs=[word_output])
|
84 |
|
85 |
with gr.Column():
|
86 |
with gr.Accordion("Word to PDF"):
|
87 |
+
word_input = gr.File(label="Upload Word", file_types=[".docx"])
|
88 |
convert_word_to_pdf = gr.Button("Convert to PDF")
|
89 |
+
pdf_output = gr.File(label="Download PDF file", type="filepath", file_types=[".pdf"])
|
90 |
|
91 |
convert_word_to_pdf.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])
|
92 |
|
93 |
+
app.launch()
|