Amaanali01's picture
Create app.py
3266847 verified
import gradio as gr
from docx import Document
from fpdf import FPDF
import tempfile
def convert_text(text):
# ---- Create DOCX ----
docx_path = tempfile.mktemp(suffix=".docx")
document = Document()
document.add_paragraph(text)
document.save(docx_path)
# ---- Create PDF ----
pdf_path = tempfile.mktemp(suffix=".pdf")
pdf = FPDF()
pdf.add_page()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.set_font("Arial", size=12)
for line in text.split("\n"):
pdf.multi_cell(0, 10, line)
pdf.output(pdf_path)
return docx_path, pdf_path
# ---- Guide Message ----
GUIDE_TEXT = """
# πŸ“˜ How to Use This Converter
### ✍️ Step 1: Enter text
Type or paste any text into the input textbox.
### πŸ”„ Step 2: Convert
Click the **Convert** button.
The system will generate two files:
- **DOCX** (Microsoft Word)
- **PDF** (Portable Document Format)
### πŸ“₯ Step 3: Download
After conversion, download your files by clicking:
- **Download DOCX**
- **Download PDF**
---
# βœ” Supported Formats
- Input: **Plain text**
- Output:
- **.docx** (Word Document)
- **.pdf** (PDF File)
---
If you need more formats (TXT, HTML, ZIP, etc.), just tell us!
"""
def show_guide():
return GUIDE_TEXT
with gr.Blocks() as demo:
gr.Markdown("# πŸ“ Text β†’ DOCX / PDF Converter")
with gr.Row():
guide_btn = gr.Button("πŸ“˜ Guide")
guide_box = gr.Markdown("", visible=False)
text_input = gr.Textbox(label="Enter your text here", lines=10)
convert_btn = gr.Button("Convert")
docx_file = gr.File(label="Download DOCX")
pdf_file = gr.File(label="Download PDF")
# Show guide when clicked
guide_btn.click(show_guide, outputs=guide_box)
guide_btn.click(lambda: gr.update(visible=True), None, guide_box)
# Convert button logic
convert_btn.click(
convert_text,
inputs=text_input,
outputs=[docx_file, pdf_file]
)
demo.launch()