| import gradio as gr |
| from docx import Document |
| from fpdf import FPDF |
| import tempfile |
|
|
| def convert_text(text): |
| |
| docx_path = tempfile.mktemp(suffix=".docx") |
| document = Document() |
| document.add_paragraph(text) |
| document.save(docx_path) |
|
|
| |
| 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_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") |
|
|
| |
| guide_btn.click(show_guide, outputs=guide_box) |
| guide_btn.click(lambda: gr.update(visible=True), None, guide_box) |
|
|
| |
| convert_btn.click( |
| convert_text, |
| inputs=text_input, |
| outputs=[docx_file, pdf_file] |
| ) |
|
|
| demo.launch() |
|
|