artificialguybr's picture
Update app.py
d9652e8 verified
raw
history blame
2.22 kB
import gradio as gr
from pdf2docx import Converter
from fpdf import FPDF
from docx import Document
import os
title_and_description = """
# PDF to Word and Word to PDF converter
Created by [@artificialguybr](https://artificialguy.com)
Upload a PDF file to convert to Word or a Word file to convert to PDF.
## Features
- **Easy to use**: Simple interface to upload PDF or Word files and convert to the desired format.
- **High quality**: Converts while maintaining the best possible quality.
- **Efficient processing**: Uses `pdf2docx`, `fpdf` and `docx` for fast and reliable conversions.
- **Unlimited Use**: No file limit. Use unlimited!
Feel free to use in your own documents!
"""
def pdf_to_word(pdf_file):
docx_filename = pdf_file.name.replace('.pdf', '.docx')
cv = Converter(pdf_file.name)
cv.convert(docx_filename, multi_processing=True, start=0, end=None)
cv.close()
return docx_filename
def word_to_pdf(docx_file):
pdf_filename = docx_file.name.replace('.docx', '.pdf')
document = Document(docx_file.name)
pdf = FPDF()
pdf.add_page()
for paragraph in document.paragraphs:
pdf.set_font("Arial", size = 12)
pdf.multi_cell(0, 10, paragraph.text)
pdf.output(pdf_filename)
return pdf_filename
with gr.Blocks() as app:
gr.Markdown(title_and_description)
with gr.Row():
with gr.Column():
with gr.Accordion("PDF para Word"):
pdf_input = gr.File(label="Carregar PDF")
convert_pdf_to_word = gr.Button("Converter para Word")
word_output = gr.File(label="Baixar arquivo Word", type="filepath")
convert_pdf_to_word.click(pdf_to_word, inputs=[pdf_input], outputs=[word_output])
with gr.Column():
with gr.Accordion("Word para PDF"):
word_input = gr.File(label="Carregar Word")
convert_word_to_pdf = gr.Button("Converter para PDF")
pdf_output = gr.File(label="Baixar arquivo PDF", type="filepath")
convert_word_to_pdf.click(word_to_pdf, inputs=[word_input], outputs=[pdf_output])
app.launch()