data-boards / app.py
prithivMLmods's picture
Update app.py
a9bbdc9 verified
raw
history blame
3.31 kB
import gradio as gr
from pdf2docx import Converter
from docx import Document
from fpdf import FPDF
import os
import tempfile
title_and_description = """
# PDF to Word and Word to PDF Converter
This tool allows you to convert PDF files to Word documents and Word documents to PDF files.
"""
def pdf_to_word(pdf_file):
"""
Converts a PDF file to a Word document.
"""
try:
# Create a temporary directory to store the output file
with tempfile.TemporaryDirectory() as temp_dir:
docx_filename = os.path.join(temp_dir, os.path.basename(pdf_file.name).replace('.pdf', '.docx'))
# Convert PDF to Word
cv = Converter(pdf_file.name)
cv.convert(docx_filename, multi_processing=True, start=0, end=None)
cv.close()
# Return the path to the converted file
return docx_filename
except Exception as e:
return f"Error: {e}"
def word_to_pdf(docx_file):
"""
Converts a Word document to a PDF file.
"""
try:
# Create a temporary directory to store the output file
with tempfile.TemporaryDirectory() as temp_dir:
pdf_filename = os.path.join(temp_dir, "output.pdf")
# Convert Word to PDF
doc = Document(docx_file.name)
pdf = FPDF(format='A4')
pdf.set_auto_page_break(auto=True, margin=15)
pdf.add_page()
pdf.add_font('Arial', '', 'Arial.ttf', uni=True)
pdf.set_font('Arial', size=12)
for para in doc.paragraphs:
text = para.text.strip()
if not text:
continue
words = text.split()
line = ''
for word in words:
if pdf.get_string_width(line + word) < (pdf.w - 2 * pdf.l_margin):
line += word + ' '
else:
pdf.cell(0, 10, line, ln=True)
line = word + ' '
if line:
pdf.cell(0, 10, line, ln=True)
pdf.output(pdf_filename)
# Return the path to the converted file
return pdf_filename
except Exception as e:
return f"Error: {e}"
with gr.Blocks() as app:
gr.Markdown(title_and_description)
with gr.Row():
with gr.Column():
with gr.Accordion("PDF to Word"):
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
convert_pdf_to_word = gr.Button("Convert to Word")
word_output = gr.File(label="Download Word file", type="filepath", file_types=[".docx"])
convert_pdf_to_word.click(pdf_to_word, inputs=[pdf_input], outputs=[word_output])
with gr.Column():
with gr.Accordion("Word to PDF"):
word_input = gr.File(label="Upload Word", file_types=[".docx"])
convert_word_to_pdf = gr.Button("Convert to PDF")
pdf_output = gr.File(label="Download PDF file", type="filepath", file_types=[".pdf"])
convert_word_to_pdf.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])
app.launch()