Spaces:
Sleeping
Sleeping
File size: 1,966 Bytes
81dba5a 4194e87 a1eb9dc 61cdb02 4194e87 81dba5a 4194e87 a1eb9dc 4194e87 81dba5a 4194e87 a1eb9dc 4194e87 81dba5a 4194e87 a1eb9dc 4194e87 81dba5a 4194e87 |
1 2 3 4 5 6 7 8 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 |
import gradio as gr
import fitz # PyMuPDF
from docx import Document
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
import io
# PDF to Word conversion function
def pdf_to_word(pdf_file):
# Read PDF file using PyMuPDF
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
text = ""
for page in doc:
text += page.get_text()
# Create a Word document using python-docx
docx = Document()
docx.add_paragraph(text)
# Save the Word document to a bytes buffer
buffer = io.BytesIO()
docx.save(buffer)
buffer.seek(0)
return buffer, "converted.docx"
# Word to PDF conversion function
def word_to_pdf(docx_file):
# Read Word document using python-docx
doc = Document(docx_file)
text = ""
for para in doc.paragraphs:
text += para.text + "\n"
# Create a PDF using reportlab
buffer = io.BytesIO()
c = canvas.Canvas(buffer, pagesize=letter)
textobject = c.beginText()
textobject.setTextOrigin(50, 750)
lines = text.split('\n')
for line in lines:
textobject.textLine(line)
c.drawText(textobject)
c.showPage()
c.save()
buffer.seek(0)
return buffer, "converted.pdf"
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("<h1>PDF to Word and Word to PDF Converter</h1>")
with gr.Tab("PDF to Word"):
pdf_input = gr.File(label="Upload PDF File", type="file")
pdf_convert_btn = gr.Button("Convert to Word")
word_output = gr.File(label="Download Word File")
pdf_convert_btn.click(pdf_to_word, inputs=pdf_input, outputs=word_output)
with gr.Tab("Word to PDF"):
word_input = gr.File(label="Upload Word File", type="file")
word_convert_btn = gr.Button("Convert to PDF")
pdf_output = gr.File(label="Download PDF File")
word_convert_btn.click(word_to_pdf, inputs=word_input, outputs=pdf_output)
demo.launch() |